I am using the google-map-react NPM package to create a Google Map and several markers.
Question: How can we add the default Google Maps markers to
Based on the answer by MasterAM.
If you using react hooks, this one could do the trick:
import React, { useMemo, useEffect } from 'react';
const createControlledPromise = () => {
let resolver;
let rejector;
const promise = new Promise((resolve, reject) => {
resolver = resolve;
rejector = reject;
});
return { promise, resolver, rejector };
};
const useMarker = ({ lat, lng }) => {
const {
promise: apiPromise,
resolver: handleGoogleApiLoaded
} = useMemo(
createControlledPromise,
[]
).current;
useEffect(
() => {
let marker;
apiPromise.then(api => {
const { map, maps } = api;
marker = new maps.Marker({ position: { lat, lng }, map });
});
return () => {
if (marker) {
marker.setMap(null);
}
};
},
[lat, lon],
);
return { handleGoogleApiLoaded };
};
const Location = ({
location: { locationName, lat, lng }
}) => {
const { handleGoogleApiLoaded } = useMarker({ lat, lng });
return (
{locationName}
);
};