Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (GLocationPicker): changed leaflet location picker to gmap
- Loading branch information
1 parent
c283310
commit 2cec8ae
Showing
6 changed files
with
278 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export const RECAPTCHA_SITE_KEY = "6Lc67IceAAAAADO2oNsVRxlCOdKpFJufdBiefGrf"; | ||
|
||
export const GMAPS_API_KEY = "AIzaSyDsBAc3y7deI5ZO3NtK5GuzKwtUzQNJNUk"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import React from "react"; | ||
import { Wrapper, Status } from "@googlemaps/react-wrapper"; | ||
import { createCustomEqual } from "fast-equals"; | ||
import { isLatLngLiteral } from "@googlemaps/typescript-guards"; | ||
|
||
import { GMAPS_API_KEY } from "../../Common/env"; | ||
|
||
const render = (status: Status) => { | ||
return <h1>{status}</h1>; | ||
}; | ||
|
||
interface GLocationPickerProps { | ||
lat: number; | ||
lng: number; | ||
handleOnChange: (location: google.maps.LatLng) => void; | ||
} | ||
|
||
const GLocationPicker = ({ | ||
lat, | ||
lng, | ||
handleOnChange, | ||
}: GLocationPickerProps) => { | ||
const [location, setLocation] = React.useState<google.maps.LatLng | null>( | ||
null | ||
); | ||
const [zoom, setZoom] = React.useState(4); | ||
const [center, setCenter] = React.useState<google.maps.LatLngLiteral>({ | ||
lat, | ||
lng, | ||
}); | ||
|
||
React.useEffect(() => { | ||
(async function () { | ||
const latLng = await new google.maps.LatLng(lat, lng); | ||
setLocation(latLng); | ||
})(); | ||
}, [lat, lng]); | ||
|
||
const onClick = (e: google.maps.MapMouseEvent) => { | ||
handleOnChange(e.latLng); | ||
}; | ||
|
||
const onIdle = (m: google.maps.Map) => { | ||
console.log("onIdle"); | ||
setZoom(m.getZoom()!); | ||
setCenter(m.getCenter()!.toJSON()); | ||
}; | ||
|
||
return ( | ||
<div className="flex w-96 h-96"> | ||
<Wrapper apiKey={GMAPS_API_KEY} render={render}> | ||
<Map | ||
center={center} | ||
onClick={onClick} | ||
onIdle={onIdle} | ||
zoom={zoom} | ||
style={{ flexGrow: "1", height: "100%" }} | ||
> | ||
{location && <Marker position={location} />} | ||
</Map> | ||
</Wrapper> | ||
</div> | ||
); | ||
}; | ||
interface MapProps extends google.maps.MapOptions { | ||
style: { [key: string]: string }; | ||
onClick?: (e: google.maps.MapMouseEvent) => void; | ||
onIdle?: (map: google.maps.Map) => void; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const Map: React.FC<MapProps> = ({ | ||
onClick, | ||
onIdle, | ||
children, | ||
style, | ||
...options | ||
}) => { | ||
const ref = React.useRef<HTMLDivElement>(null); | ||
const [map, setMap] = React.useState<google.maps.Map>(); | ||
|
||
React.useEffect(() => { | ||
if (ref.current && !map) { | ||
setMap(new window.google.maps.Map(ref.current, {})); | ||
} | ||
}, [ref, map]); | ||
|
||
useDeepCompareEffectForMaps(() => { | ||
if (map) { | ||
map.setOptions(options); | ||
} | ||
}, [map, options]); | ||
|
||
React.useEffect(() => { | ||
if (map) { | ||
["click", "idle"].forEach((eventName) => | ||
google.maps.event.clearListeners(map, eventName) | ||
); | ||
|
||
if (onClick) { | ||
map.addListener("click", onClick); | ||
} | ||
|
||
if (onIdle) { | ||
map.addListener("idle", () => onIdle(map)); | ||
} | ||
} | ||
}, [map, onClick, onIdle]); | ||
|
||
return ( | ||
<> | ||
<div ref={ref} style={style} /> | ||
{React.Children.map(children, (child) => { | ||
if (React.isValidElement(child)) { | ||
return React.cloneElement(child, { map }); | ||
} | ||
})} | ||
</> | ||
); | ||
}; | ||
|
||
const Marker: React.FC<google.maps.MarkerOptions> = (options) => { | ||
const [marker, setMarker] = React.useState<google.maps.Marker>(); | ||
|
||
React.useEffect(() => { | ||
if (!marker) { | ||
setMarker(new google.maps.Marker()); | ||
} | ||
|
||
return () => { | ||
if (marker) { | ||
marker.setMap(null); | ||
} | ||
}; | ||
}, [marker]); | ||
|
||
React.useEffect(() => { | ||
if (marker) { | ||
marker.setOptions(options); | ||
} | ||
}, [marker, options]); | ||
|
||
return null; | ||
}; | ||
|
||
const deepCompareEqualsForMaps = createCustomEqual( | ||
// @ts-ignore | ||
(deepEqual) => (a: any, b: any) => { | ||
if ( | ||
isLatLngLiteral(a) || | ||
a instanceof google.maps.LatLng || | ||
isLatLngLiteral(b) || | ||
b instanceof google.maps.LatLng | ||
) { | ||
return new google.maps.LatLng(a).equals(new google.maps.LatLng(b)); | ||
} | ||
|
||
// @ts-ignore | ||
return deepEqual(a, b); | ||
} | ||
); | ||
|
||
function useDeepCompareMemoize(value: any) { | ||
const ref = React.useRef(); | ||
|
||
if (!deepCompareEqualsForMaps(value, ref.current)) { | ||
ref.current = value; | ||
} | ||
|
||
return ref.current; | ||
} | ||
|
||
function useDeepCompareEffectForMaps( | ||
callback: React.EffectCallback, | ||
dependencies: any[] | ||
) { | ||
React.useEffect(callback, dependencies.map(useDeepCompareMemoize)); | ||
} | ||
|
||
export default GLocationPicker; |
Oops, something went wrong.