Skip to content

Commit 035d3f7

Browse files
sanghunlee-711cloudjosworks27
authored
Add Polyline component and sample in README.md (#59)
* feat: polyline component and sample * feat: up version * update package.json * chore: coding convention 통일 --------- Co-authored-by: cloud <cloud@oysterable.com> Co-authored-by: Seongcheol Jo <48130830+josworks27@users.noreply.github.com> Co-authored-by: Seongcheol Jo <josworks27@gmail.com>
1 parent b2339e2 commit 035d3f7

4 files changed

Lines changed: 112 additions & 13 deletions

File tree

README.md

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# react-naver-map
22

33
## Introduction
4+
45
네이버 맵을 리액트에서 사용하기 편하게 만들어 둔 라이브러리입니다.
56

67
## Installation
@@ -17,27 +18,71 @@ pnpm add @r2don/react-naver-map
1718
```
1819

1920
## Usage
21+
2022
> 기본 스타일이 적용되어있지 않으니, 필요에 맞게 width와 height를 적용해주세요.
23+
2124
### Map component
2225

2326
```js
24-
import { Map, useNaverMapInit, Marker } from '@r2don/react-naver-map'
27+
import { Map, useNaverMapInit, Marker } from "@r2don/react-naver-map";
2528

2629
const MARKERS = [
27-
{latitude: 37., longitude: 127},
28-
{latitude: 37.5, longitude: 127.5},
29-
{latitude: 38, longitude: 128},
30-
{latitude: 38.5, longitude: 128.5},
31-
]
30+
{ latitude: 37, longitude: 127 },
31+
{ latitude: 37.5, longitude: 127.5 },
32+
{ latitude: 38, longitude: 128 },
33+
{ latitude: 38.5, longitude: 128.5 },
34+
];
35+
36+
function App() {
37+
const { isLoaded } = useNaverMapInit();
38+
39+
if (!isLoaded) return null;
40+
41+
return (
42+
<Map
43+
center={{ latitude: 37.3595704, longitude: 127.105399 }}
44+
style={{ height: "500px", width: "500px" }}
45+
>
46+
{MARKERS.map((marker) => (
47+
<Marker key={JSON.stringify(marker)} position={marker} />
48+
))}
49+
</Map>
50+
);
51+
}
52+
```
53+
54+
### PolyLine Component
55+
56+
```js
57+
import { Map, useNaverMapInit, Marker, Poly } from "@r2don/react-naver-map";
58+
59+
const MARKERS = [
60+
{ latitude: 37, longitude: 127 },
61+
{ latitude: 37.5, longitude: 127.5 },
62+
{ latitude: 38, longitude: 128 },
63+
{ latitude: 38.5, longitude: 128.5 },
64+
];
65+
66+
function App() {
67+
const { isLoaded } = useNaverMapInit();
3268

33-
function App () {
34-
const {isLoaded} = useNaverMapInit();
69+
if (!isLoaded) return null;
3570

36-
if(!isLoaded) return null;
71+
const path = [{ latitude: 37.3595704; longitude: 127.105399 }, { latitude: 37.3585781; longitude: 127.1053234 }];
72+
const mid = {
73+
latitude: (path[0].latitude + path[1].latitude) / 2,
74+
longitude: (path[0].longitude + path[1].longitude) / 2,
75+
}
3776

3877
return (
39-
<Map center={{latitude: 37.3595704, longitude: 127.105399}} style={{height: '500px', width: '500px'}}>
40-
{MARKERS.map((marker) => <Marker key={JSON.stringify(marker)} position={marker} />)}
78+
<Map
79+
center={{ latitude: mid.latitude, longitude: mid.longitude }}
80+
style={{ height: "500px", width: "500px" }}
81+
>
82+
{MARKERS.map((marker) => (
83+
<Marker key={JSON.stringify(marker)} position={marker} />
84+
))}
85+
<Polyline path={path} />
4186
</Map>
4287
);
4388
}
@@ -47,4 +92,4 @@ function App () {
4792

4893
Licensed under the MIT License, Copyright (c) 2022 r2don.
4994

50-
See [LICENSE](https://github.com/r2don/react-naver-map/blob/main/LICENSE) for more information.
95+
See [LICENSE](https://github.com/r2don/react-naver-map/blob/main/LICENSE) for more information.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@r2don/react-naver-map",
3-
"version": "0.0.18",
3+
"version": "0.0.19",
44
"author": {
55
"name": "r2don",
66
"email": "leejj2002@naver.com"

src/components/Polyline.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useRef } from "react";
2+
import { useMapContext } from "../context";
3+
import { useIsomorphicLayoutEffect } from "../hooks/useIsomorphicLayoutEffect";
4+
5+
interface PolylineProps {
6+
path: { latitude: number; longitude: number }[];
7+
strokeColor?: string;
8+
strokeWeight?: number;
9+
strokeOpacity?: number;
10+
strokeStyle?: naver.maps.strokeStyleType;
11+
}
12+
13+
/**
14+
* Set polyline into Map obejct without rendering anything in VirtualDOM
15+
* @returns <></>
16+
*/
17+
const Polyline = ({
18+
path,
19+
strokeColor = "#FF0000",
20+
strokeWeight = 4,
21+
strokeOpacity = 0.8,
22+
strokeStyle = "solid",
23+
}: PolylineProps) => {
24+
const map = useMapContext();
25+
const polylineRef = useRef<naver.maps.Polyline | null>(null);
26+
27+
useIsomorphicLayoutEffect(() => {
28+
if (!map) return;
29+
30+
const polylinePath = path.map(
31+
(point) => new naver.maps.LatLng(point.latitude, point.longitude),
32+
);
33+
34+
polylineRef.current = new naver.maps.Polyline({
35+
map,
36+
path: polylinePath,
37+
strokeColor,
38+
strokeWeight,
39+
strokeOpacity,
40+
strokeStyle: strokeStyle as naver.maps.strokeStyleType,
41+
});
42+
43+
return () => {
44+
if (polylineRef.current) {
45+
polylineRef.current.setMap(null);
46+
}
47+
};
48+
}, [map, path, strokeColor, strokeWeight, strokeOpacity, strokeStyle]);
49+
50+
return <></>;
51+
};
52+
53+
export default Polyline;

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./Map";
22
export * from "./Controls";
33
export * from "./marker";
44
export * from "./Cluster";
5+
export * from "./Polyline";

0 commit comments

Comments
 (0)