마커 클러스터화하기
공식 예제 대응네이버 공식 예제 보기 ↗
많은 마커를 클러스터로 묶어 지도에 표시합니다. react-naver-maps 컴포넌트 대신 SDK 객체를 직접 사용합니다.
코드 접기코드 보기
import { useEffect, useRef } from 'react';
import {
NavermapsProvider,
Container as MapDiv,
NaverMap,
useMap,
useNavermaps,
} from 'react-naver-maps';
import { makeMarkerClustering } from '../samples/marker-cluster';
import { accidentDeath } from '../samples/accident-death';
function ClusterMap() {
const navermaps = useNavermaps();
const map = useMap();
const clusterRef = useRef<any>(null);
useEffect(() => {
const MarkerClustering = makeMarkerClustering(window.naver);
const data = accidentDeath.searchResult.accidentDeath;
const markers = data.map((d: any) => {
return new navermaps.Marker({
position: new navermaps.LatLng(
parseFloat(d.grd_la),
parseFloat(d.grd_lo),
),
draggable: true,
});
});
const htmlMarker1 = {
content:
'<div style="cursor:pointer;width:40px;height:40px;line-height:42px;font-size:10px;color:white;text-align:center;font-weight:bold;background:url(https://navermaps.github.io/maps.js.ncp/docs/img/cluster-marker-1.png);background-size:contain;"></div>',
size: new navermaps.Size(40, 40),
anchor: new navermaps.Point(20, 20),
};
const htmlMarker2 = {
content:
'<div style="cursor:pointer;width:40px;height:40px;line-height:42px;font-size:10px;color:white;text-align:center;font-weight:bold;background:url(https://navermaps.github.io/maps.js.ncp/docs/img/cluster-marker-2.png);background-size:contain;"></div>',
size: new navermaps.Size(40, 40),
anchor: new navermaps.Point(20, 20),
};
const htmlMarker3 = {
content:
'<div style="cursor:pointer;width:40px;height:40px;line-height:42px;font-size:10px;color:white;text-align:center;font-weight:bold;background:url(https://navermaps.github.io/maps.js.ncp/docs/img/cluster-marker-3.png);background-size:contain;"></div>',
size: new navermaps.Size(40, 40),
anchor: new navermaps.Point(20, 20),
};
const htmlMarker4 = {
content:
'<div style="cursor:pointer;width:40px;height:40px;line-height:42px;font-size:10px;color:white;text-align:center;font-weight:bold;background:url(https://navermaps.github.io/maps.js.ncp/docs/img/cluster-marker-4.png);background-size:contain;"></div>',
size: new navermaps.Size(40, 40),
anchor: new navermaps.Point(20, 20),
};
const htmlMarker5 = {
content:
'<div style="cursor:pointer;width:40px;height:40px;line-height:42px;font-size:10px;color:white;text-align:center;font-weight:bold;background:url(https://navermaps.github.io/maps.js.ncp/docs/img/cluster-marker-5.png);background-size:contain;"></div>',
size: new navermaps.Size(40, 40),
anchor: new navermaps.Point(20, 20),
};
clusterRef.current = new MarkerClustering({
minClusterSize: 2,
maxZoom: 8,
map: map,
markers: markers,
disableClickZoom: false,
gridSize: 120,
icons: [htmlMarker1, htmlMarker2, htmlMarker3, htmlMarker4, htmlMarker5],
indexGenerator: [10, 100, 200, 500, 1000],
stylingFunction: (clusterMarker: any, count: number) => {
clusterMarker.getElement().querySelector('div').textContent =
count.toString();
},
});
return () => {
if (clusterRef.current) {
clusterRef.current.setMap(null);
}
};
}, [map, navermaps]);
return null;
}
function ClusterMapContent() {
const navermaps = useNavermaps();
return (
<NaverMap
defaultCenter={{ lat: 36.2253017, lng: 127.6460516 }}
defaultZoom={6}
zoomControl
zoomControlOptions={{
position: navermaps.Position.TOP_LEFT,
style: navermaps.ZoomControlStyle.SMALL,
}}
>
<ClusterMap />
</NaverMap>
);
}
function ClusterMapContainer() {
return (
<MapDiv style={{ width: '100%', height: '600px' }}>
<ClusterMapContent />
</MapDiv>
);
}
export default function MarkerClusterExample() {
return (
<NavermapsProvider ncpKeyId="aluya4ff04">
<ClusterMapContainer />
</NavermapsProvider>
);
}설명
NaverMap의 자식 컴포넌트에서 useMap()으로 지도 인스턴스를 받아 useEffect 안에서 navermaps.Marker와 MarkerClustering을 직접 생성합니다. 서드파티 유틸리티 등을 사용할 때 이와 같이 SDK 객체에 직접 접근할 수 있습니다.
복사해 쓸 때 함께 필요한 파일
이 예제는 사이트의 보조 코드를 씁니다. 아래 파일도 같은 경로에 두거나 내용을 옮겨 주세요.
React로 옮기며 바꾼 점
| 네이버 공식 예제 | 이 예제 |
|---|---|
$.getScript로 MarkerClustering.js와 데이터 파일을 받은 뒤 onLoad 실행 | makeMarkerClustering·accidentDeath를 모듈로 import |
전역 스크립트에서 new MarkerClustering 생성 | useMap()으로 받은 지도에 useEffect 안에서 생성하고, 정리 함수에서 setMap(null) |
stylingFunction에서 jQuery로 클러스터 숫자 넣기 | querySelector·textContent |
new naver.maps.Map의 center·zoom·zoomControl 옵션 | NaverMap의 defaultCenter·defaultZoom·zoomControl prop |