From d470a08e9ce329f1a6ff9a4591c41e1e04fb62bb Mon Sep 17 00:00:00 2001
From: Aubrey-Li <63608597+Aubrey-Li@users.noreply.github.com>
Date: Sat, 31 Jul 2021 12:36:12 -0700
Subject: allow locating user location

---
 src/client/views/nodes/MapBox/MapBox.tsx | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

(limited to 'src/client/views/nodes/MapBox/MapBox.tsx')

diff --git a/src/client/views/nodes/MapBox/MapBox.tsx b/src/client/views/nodes/MapBox/MapBox.tsx
index 2b3bcb0a2..6447d9715 100644
--- a/src/client/views/nodes/MapBox/MapBox.tsx
+++ b/src/client/views/nodes/MapBox/MapBox.tsx
@@ -69,7 +69,7 @@ export class MapBox extends ViewBoxAnnotatableComponent<ViewBoxAnnotatableProps
     @observable private _map: google.maps.Map = null as unknown as google.maps.Map;
     @observable private selectedPlace: MapMarker | undefined;
     @observable private markerMap: { [id: string]: google.maps.Marker } = {};
-    @observable private center = defaultCenter;
+    @observable private center = navigator.geolocation ? navigator.geolocation.getCurrentPosition : defaultCenter;
     @observable private zoom = 2.5;
     @observable private infoWindowOpen = false;
     @observable private bounds = new window.google.maps.LatLngBounds();
@@ -107,6 +107,19 @@ export class MapBox extends ViewBoxAnnotatableComponent<ViewBoxAnnotatableProps
     private loadHandler = (map: google.maps.Map) => {
         this._map = map;
         drawingManager.setMap(map);
+        if (navigator.geolocation) {
+            navigator.geolocation.getCurrentPosition(
+                (position: GeolocationPosition) => {
+                    const pos = {
+                        lat: position.coords.latitude,
+                        lng: position.coords.longitude,
+                    };
+                    this._map.setCenter(pos);
+                }
+            );
+        } else {
+            alert("Your geolocation is not supported by browser.")
+        }
         this.fitBounds(map);
     }
 
@@ -268,7 +281,7 @@ export class MapBox extends ViewBoxAnnotatableComponent<ViewBoxAnnotatableProps
                 <GoogleMap
                     mapContainerStyle={mapContainerStyle}
                     zoom={this.zoom}
-                    center={this.center}
+                    // center={this.center}
                     onLoad={map => this.loadHandler(map)}
                     options={mapOptions}
                 >
-- 
cgit v1.2.3-70-g09d2

