지도 범위를 재설정합니다. 어떤 좌표나 마커들이 지도에 모두 보여야 할 때 좌표들의 정보를 갖는 LatLngBounds를 사용하여 좌표들이 모두 보이게 지도의 중심좌표와 레벨을 재설정 할 수 있습니다.
var mapContainer = document.getElementById('map'), // 지도를 표시할 div
mapOption = {
center: new kakao.maps.LatLng(33.450701, 126.570667), // 지도의 중심좌표
level: 3 // 지도의 확대 레벨
};
var map = new kakao.maps.Map(mapContainer, mapOption); // 지도를 생성합니다
// 버튼을 클릭하면 아래 배열의 좌표들이 모두 보이게 지도 범위를 재설정합니다
var points = [
new kakao.maps.LatLng(33.452278, 126.567803),
new kakao.maps.LatLng(33.452671, 126.574792),
new kakao.maps.LatLng(33.451744, 126.572441)
];
// 지도를 재설정할 범위정보를 가지고 있을 LatLngBounds 객체를 생성합니다
var bounds = new kakao.maps.LatLngBounds();
var i, marker;
for (i = 0; i < points.length; i++) {
// 배열의 좌표들이 잘 보이게 마커를 지도에 추가합니다
marker = new kakao.maps.Marker({ position : points[i] });
marker.setMap(map);
// LatLngBounds 객체에 좌표를 추가합니다
bounds.extend(points[i]);
}
function setBounds() {
// LatLngBounds 객체에 추가된 좌표들을 기준으로 지도의 범위를 재설정합니다
// 이때 지도의 중심좌표와 레벨이 변경될 수 있습니다
map.setBounds(bounds);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>지도 범위 재설정 하기</title>
</head>
<body>
<div id="map" style="width:100%;height:350px;"></div>
<p>
<button onclick="setBounds()">지도 범위 재설정 하기</button>
</p>
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=발급받은 APP KEY를 사용하세요"></script>
<script>
var mapContainer = document.getElementById('map'), // 지도를 표시할 div
mapOption = {
center: new kakao.maps.LatLng(33.450701, 126.570667), // 지도의 중심좌표
level: 3 // 지도의 확대 레벨
};
var map = new kakao.maps.Map(mapContainer, mapOption); // 지도를 생성합니다
// 버튼을 클릭하면 아래 배열의 좌표들이 모두 보이게 지도 범위를 재설정합니다
var points = [
new kakao.maps.LatLng(33.452278, 126.567803),
new kakao.maps.LatLng(33.452671, 126.574792),
new kakao.maps.LatLng(33.451744, 126.572441)
];
// 지도를 재설정할 범위정보를 가지고 있을 LatLngBounds 객체를 생성합니다
var bounds = new kakao.maps.LatLngBounds();
var i, marker;
for (i = 0; i < points.length; i++) {
// 배열의 좌표들이 잘 보이게 마커를 지도에 추가합니다
marker = new kakao.maps.Marker({ position : points[i] });
marker.setMap(map);
// LatLngBounds 객체에 좌표를 추가합니다
bounds.extend(points[i]);
}
function setBounds() {
// LatLngBounds 객체에 추가된 좌표들을 기준으로 지도의 범위를 재설정합니다
// 이때 지도의 중심좌표와 레벨이 변경될 수 있습니다
map.setBounds(bounds);
}
</script>
</body>
</html>