Skip to content

Commit cc61113

Browse files
committed
Add marker functionality - part II
1 parent 5c8d4e8 commit cc61113

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

google-maps/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"typescript": "~3.7.2"
1818
},
1919
"scripts": {
20-
"start": "react-scripts start",
20+
"start": "PORT=8443 react-scripts start",
2121
"build": "react-scripts build",
2222
"test": "react-scripts test",
2323
"eject": "react-scripts eject"

google-maps/public/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
name="description"
1010
content="Web site created using create-react-app"
1111
/>
12-
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
1312
<!--
1413
manifest.json provides metadata used when your web app is installed on a
1514
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/

google-maps/public/manifest.json

-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
"src": "favicon.ico",
77
"sizes": "64x64 32x32 24x24 16x16",
88
"type": "image/x-icon"
9-
},
10-
{
11-
"src": "logo192.png",
12-
"type": "image/png",
13-
"sizes": "192x192"
14-
},
15-
{
16-
"src": "logo512.png",
17-
"type": "image/png",
18-
"sizes": "512x512"
199
}
2010
],
2111
"start_url": ".",

google-maps/src/Map/Map.tsx

+56
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@ interface IMap {
66
mapTypeControl?: boolean;
77
}
88

9+
interface IMarker {
10+
address: string;
11+
latitude: number;
12+
longitude: number;
13+
}
14+
915
type GoogleLatLng = google.maps.LatLng;
1016
type GoogleMap = google.maps.Map;
17+
type GoogleMarker = google.maps.Marker;
1118

1219
const Map: React.FC<IMap> = ({ mapType, mapTypeControl = false}) => {
1320

1421
const ref = useRef<HTMLDivElement>(null);
1522
const [map, setMap] = useState<GoogleMap>();
23+
const [marker, setMarker] = useState<IMarker>();
1624

1725
const startMap = (): void => {
1826
if (!map) {
@@ -26,6 +34,54 @@ const Map: React.FC<IMap> = ({ mapType, mapTypeControl = false}) => {
2634
initMap(4, defaultAddress);
2735
};
2836

37+
const initEventListener = ():void => {
38+
if (map) {
39+
google.maps.event.addListener(map, 'click', function(e) {
40+
coordinateToAddress(e.latLng);
41+
})
42+
}
43+
};
44+
useEffect(initEventListener, [map]);
45+
46+
const coordinateToAddress = async (coordinate: GoogleLatLng) => {
47+
const geocoder = new google.maps.Geocoder();
48+
await geocoder.geocode({ location: coordinate}, function (results, status) {
49+
if (status === 'OK') {
50+
setMarker({
51+
address: results[0].formatted_address,
52+
latitude: coordinate.lat(),
53+
longitude: coordinate.lng()
54+
})
55+
}
56+
});
57+
};
58+
59+
const addSingleMarker = ():void => {
60+
if (marker) {
61+
addMarker(new google.maps.LatLng(marker.latitude, marker.longitude));
62+
}
63+
};
64+
useEffect(addSingleMarker, [marker]);
65+
66+
const addMarker = (location: GoogleLatLng): void => {
67+
const marker:GoogleMarker = new google.maps.Marker({
68+
position: location,
69+
map: map,
70+
icon: getIconAttributes('#000000')
71+
});
72+
};
73+
74+
const getIconAttributes = (iconColor: string) => {
75+
return {
76+
path: 'M11.0639 15.3003L26.3642 2.47559e-05L41.6646 15.3003L26.3638 51.3639L11.0639 15.3003 M22,17.5a4.5,4.5 0 1,0 9,0a4.5,4.5 0 1,0 -9,0Z',
77+
fillColor: iconColor,
78+
fillOpacity: 0.8,
79+
strokeColor: 'pink',
80+
strokeWeight: 2,
81+
anchor: new google.maps.Point(30, 50)
82+
};
83+
};
84+
2985
const initMap = (zoomLevel: number, address: GoogleLatLng): void => {
3086
if (ref.current) {
3187
setMap(

0 commit comments

Comments
 (0)