이미지 아이콘 레티나 대응하기
공식 예제 대응네이버 공식 예제 보기 ↗
큰 이미지를 줄여 레티나 화면에서도 선명하게 마커 아이콘을 표시합니다.
코드 접기코드 보기
import {
NavermapsProvider,
Container as MapDiv,
NaverMap,
Marker,
} from 'react-naver-maps';
const position = { lat: 37.3595704, lng: 127.105399 };
// 원본 이미지는 50x68입니다. scaledSize로 절반(25x34)으로 줄이면 레티나에서 선명합니다.
const icon = {
url: 'https://navermaps.github.io/maps.js.ncp/docs/img/example/ico_pin.jpg',
size: { width: 25, height: 34 },
scaledSize: { width: 25, height: 34 },
origin: { x: 0, y: 0 },
anchor: { x: 12, y: 34 },
};
function MarkerRetinaMapContent() {
return (
<NaverMap defaultCenter={position} defaultZoom={15}>
<Marker position={position} icon={icon} />
</NaverMap>
);
}
export default function MarkerRetinaExample() {
return (
<NavermapsProvider ncpKeyId="aluya4ff04">
<MapDiv style={{ width: '100%', height: '400px' }}>
<MarkerRetinaMapContent />
</MapDiv>
</NavermapsProvider>
);
}설명
icon.scaledSize에 보여줄 크기를 주면 원본 이미지(50x68)를 절반으로 줄여 그려 레티나에서 선명합니다.
React로 옮기며 바꾼 점
| 네이버 공식 예제 | 이 예제 |
|---|---|
new naver.maps.Marker에 map·icon 옵션 | <Marker>의 icon prop |
new naver.maps.Size·new naver.maps.Point로 size·scaledSize·origin·anchor | { width, height }·{ x, y } 리터럴 |