Skip to content

Commit

Permalink
Fetch user current location
Browse files Browse the repository at this point in the history
  • Loading branch information
gupta-arpan committed May 24, 2023
1 parent 590797c commit ddce996
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/components/Location.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { GoogleMap, useLoadScript, MarkerF } from "@react-google-maps/api";
import { useMemo, useState } from "react";

function Location() {
const { isLoaded } = useLoadScript({
googleMapsApiKey: import.meta.env.VITE_REACT_APP_GOOGLE_MAP_API_KEY,
});

if (!isLoaded) return <div>Loading...</div>;
return <Map />
}

function Map() {

//const center = useMemo(() => ({ lat: 26.86247, lng: 75.76241 }),[]);
const [currentLocation, setCurrentLocation] = useState({
lat: 26.86247,
lng: 75.76241,
});

const getUserLocation = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const userLocation = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
setCurrentLocation(userLocation); // ADDED
});
} else {
// code for legacy browsers
}
};

return (
<GoogleMap
zoom={20}
center={currentLocation}
mapContainerClassName="w-full h-screen"
>
<MarkerF onLoad={getUserLocation} position={currentLocation} />
</GoogleMap>
);
}

export default Location;

0 comments on commit ddce996

Please sign in to comment.