MapContainer.js
1.65 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
import React, { useEffect } from "react";
const { kakao } = window;
console.log(kakao);
console.log(window);
const MapContainer = ({ searchPlace }) => {
useEffect(() => {
var infowindow = new kakao.maps.InfoWindow({ zIndex: 1 });
const container = document.getElementById("myMap");
const options = {
center: new kakao.maps.LatLng(33.450701, 126.570667),
level: 3,
};
const map = new kakao.maps.Map(container, options);
const ps = new kakao.maps.services.Places();
ps.keywordSearch(searchPlace, placesSearchCB);
function placesSearchCB(data, status, pagination) {
if (status === kakao.maps.services.Status.OK) {
let bounds = new kakao.maps.LatLngBounds();
for (let i = 0; i < data.length; i++) {
displayMarker(data[i]);
bounds.extend(new kakao.maps.LatLng(data[i].y, data[i].x));
}
map.setBounds(bounds);
}
}
function displayMarker(place) {
let marker = new kakao.maps.Marker({
map: map,
position: new kakao.maps.LatLng(place.y, place.x),
});
// 마커에 클릭이벤트를 등록합니다
kakao.maps.event.addListener(marker, "click", function () {
// 마커를 클릭하면 장소명이 인포윈도우에 표출됩니다
infowindow.setContent(
'<div style="padding:5px;font-size:12px;">' +
place.place_name +
"</div>"
);
infowindow.open(map, marker);
});
}
}, [searchPlace]);
return (
<div
id="myMap"
style={{
width: "500px",
height: "500px",
}}
></div>
);
};
export default MapContainer;