I am having trouble while rendering multiple markers on the google map through the map function. One marker should render based on 'source' state and one should render based on destination state. When I'm typing in the source input the marker is rendering on the lats and lngs of source. But when I'm typing in the destination input it is shifting to the lats and lngs of destination.
Here is the map.js:
"use client";
import React, { useContext, useEffect, useState } from "react";
import {
DirectionsRenderer,
GoogleMap,
MarkerF,
OverlayView,
OverlayViewF,
useJsApiLoader,
} from "@react-google-maps/api";
import { SourceContext } from "../../context/SourceContext";
import { DestinationContext } from "../../context/DestinationContext";
function Map() {
const containerStyle = {
width: "100%",
height: window.innerWidth * 0.8,
};
const { source, setSource } = useContext(SourceContext);
const { destination, setDestination } = useContext(DestinationContext);
const [center, setCenter] = useState({
lat: -3.745,
lng: -38.523,
});
const [map, setMap] = React.useState(null);
const [directionRoutePoints, setDirectionRoutePoints] = useState([]);
useEffect(() => {
if (source && source.length !== 0 && map) {
map.panTo({
lat: source.lat,
lng: source.lng,
});
setCenter({
lat: source.lat,
lng: source.lng,
});
}
if (source.length != [] && destination.length != []) {
console.log("DIE");
directionRoute();
}
}, [source]);
/**
* Used when Destination Value Available
*/
useEffect(() => {
if (destination?.length != [] && map) {
setCenter({
lat: destination.lat,
lng: destination.lng,
});
}
if (source.length != [] && destination.length != []) {
console.log("DIE");
directionRoute();
}
}, [destination]);
/**
* Used to Get Direction Router Points
*/
const directionRoute = () => {
const DirectionsService = new google.maps.DirectionsService();
console.log("DIE");
DirectionsService.route(
{
origin: { lat: source.lat, lng: source.lng },
destination: { lat: destination.lat, lng: destination.lng },
travelMode: google.maps.TravelMode.DRIVING,
},
(result, status) => {
if (status === google.maps.DirectionsStatus.OK) {
setDirectionRoutePoints(result);
} else {
console.error("Error");
}
}
);
};
return (
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={11}
onLoad={(map) => setMap(map)}
options={{ mapId: "4113717585f11867" }}
>
{source.length != [] ? (
<MarkerF
position={{ lat: source.lat, lng: source.lng }}
icon={{
url: "/source.png",
scaledSize: {
width: 20,
height: 20,
},
}}
>
<OverlayViewF
position={{ lat: source.lat, lng: source.lng }}
mapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}
>
<div className="p-2 bg-white font-bold inline-block">
<p className="text-black text-[18px]">{source.label}</p>
</div>
</OverlayViewF>
</MarkerF>
) : null}
{destination.length != [] ? (
<MarkerF
position={{ lat: destination.lat, lng: destination.lng }}
icon={{
url: "/dest.png",
scaledSize: {
width: 20,
height: 20,
},
}}
>
<OverlayViewF
position={{ lat: destination.lat, lng: destination.lng }}
mapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}
>
<div className="p-2 bg-white font-bold inline-block">
<p className="text-black text-[18px]">{destination.label}</p>
</div>
</OverlayViewF>
</MarkerF>
) : null}
{/* <DirectionsRenderer
directions={directionRoutePoints}
options={{
polylineOptions: {
strokeColor: "#000",
strokeWeight: 5,
},
suppressMarkers: true,
}}
/> */}
</GoogleMap>
);
}
export default Map;
I've tried logging the source and destination states on console, it seems like the source state is being erased when destination is inputted.