-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkakaoMap.html
More file actions
90 lines (78 loc) · 3.09 KB
/
kakaoMap.html
File metadata and controls
90 lines (78 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>지도 생성하기</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript" src="//dapi.kakao.com/v2/maps/sdk.js?appkey=323e0a6314c61fcc89f8a85709c9eccf&libraries=services"></script>
<script>
var map, marker, markers = [];
window.onload = function () {
var mapContainer = document.getElementById('map'),
mapOption = {
center: new kakao.maps.LatLng(35.846987, 127.129922),
level: 3
};
map = new kakao.maps.Map(mapContainer, mapOption);
// 마커 전역 변수 및 배열 선언
var markerPosition = new kakao.maps.LatLng(35.846987, 127.129922);
marker = new kakao.maps.Marker({ position: markerPosition });
marker.setMap(map);
// 중심 이동
window.setCenter = function(lat, lng) {
marker.setMap(null);
var mp = new kakao.maps.LatLng(lat, lng);
map.setCenter(mp);
marker = new kakao.maps.Marker({ position: mp });
marker.setMap(map);
}
// 마커 지우기
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = [];
}
// 카테고리 별 장소 검색
window.searchCategory = function(category) {
clearMarkers();
var places = new kakao.maps.services.Places();
// 장소 검색 라이브러리(category: 파라미터, fuction: 콜백함수(함수 끝난 후 실행)
places.categorySearch(category, function (result, status) {
// status와 result를 반환
if (status === kakao.maps.services.Status.OK) {
result.forEach(function (place) {
// 각 장소에 마커를 찍음
var position = new kakao.maps.LatLng(place.y, place.x);
var m = new kakao.maps.Marker({ position: position });
m.setMap(map);
markers.push(m);
});
if (result.length > 0) {
// 검색 결과가 하나라도 있다면 그 지역을 중심으로 센터 설정
window.setCenter(result[0].y, result[0].x);
}
}
else {
alert("장소가 없습니다.");
}
// 지금 위치에서 5킬로미터 반경
}, { location: map.getCenter(), radius: 5000 });
}
};
</script>
</body>
</html>