I'm building an application that update the markers each time the user move the map.
I'm using the method onBoundChanges to get the bounds and store them in a hooks (setBounds(mapBounds).)
Unfortunately when I try to store it in a hook with setBounds(mapBounds), its sends the application in a loop.
Do you have any idea how to get around the problem?
Thank you
Here is my main function
import React, { useState, useEffect, Fragment } from "react";
import {
GoogleMap,
LoadScript,
Marker,
InfoWindow,
} from "#react-google-maps/api";
const containerStyle = {
Display: "flex",
height: "100vh",
width: "100%",
};
const divStyle = {
background: `white`,
border: `1px solid #ccc`,
padding: 15,
};
const onLoad = (infoWindow) => {
console.log("infoWindow: ", infoWindow);
};
const position = {
lat: 43.6532,
lng: -79.3832,
};
const center = {
lat: 43.6532,
lng: -79.3832,
};
function ReactMap() {
const [selected, setSelected] = useState(null);
const [bounds, setBounds] = useState(center);
const mapRef = React.useRef();
const onMapLoad = React.useCallback((map) => {
mapRef.current = map;
}, []);
const onMapBoundsChanged = React.useCallback((map) => {
const latlongchange = mapRef.current.getBounds();
const lat = latlongchange.Ua.i;
const lng = latlongchange.Ua.j;
const mapBounds = {
lat: lat,
lng: lng,
};
//setBounds(mapBounds);
console.log(mapBounds);
}, []);
return (
<Fragment>
<LoadScript googleMapsApiKey="">
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
onLoad={onMapLoad}
onBoundsChanged={onMapBoundsChanged}
zoom={14}
>
{selected ? (
<InfoWindow
onLoad={onLoad}
position={position}
onCloseClick={() => {
setSelected(null);
}}
>
<div style={divStyle}>
<h1>La place</h1>
</div>
</InfoWindow>
) : null}
<Marker
className="marker"
position={position}
name="My Marker"
color="blue"
onClick={() => {
setSelected(true);
}}
/>
</GoogleMap>
</LoadScript>
</Fragment>
);
}
export default ReactMap;
Ok. May be you should use additional hook for debouncing? For example:
Step 1: Hook useDebouncedCallback.js
import { useEffect, useRef } from 'react'
export function useDebouncedCallback(callback, wait) {
const argsRef = useRef()
const timeout = useRef()
function cleanup() {
if (timeout.current) clearTimeout(timeout.current)
}
useEffect(() => cleanup, [])
return function debouncedCallback(...args) {
argsRef.current = args
cleanup()
timeout.current = setTimeout(() => {
if (argsRef.current) callback(...argsRef.current)
}, wait)
}
}
Step 2: Your React.FC
// ...
import { useDebouncedCallback } from '#/common/hooks/useDebouncedCallback'
function ReactMap() {
// ...
const onMapBoundsChangedDebounced = useDebouncedCallback(onMapBoundsChanged, 500)
return (
<Fragment>
<LoadScript googleMapsApiKey="">
<GoogleMap
// ...
onBoundsChanged={onMapBoundsChangedDebounced}
// ...
>
Step 3: use constant center as initial state (not empty array)
const position = {
lat: 43.6532,
lng: -79.3832,
};
const center = {
lat: 43.6532,
lng: -79.3832,
};
function ReactMap() {
const [selected, setSelected] = useState(null);
const [bounds, setBounds] = useState(center);
// ...
Related
i am getting error mapRef.current.getBounds is not a function and map.getBounds is not a function when i want to getBounds of map using useRef react hook
this is my code for getting data from the csv file display marker on the map and when user scroll and zoom map then load more data from csv file and display on the marker based on change latitude and longitude of the map but i am getting error
import React, { useState, useEffect, useRef } from "react";
import { GoogleMap, LoadScript, Marker } from "#react-google-maps/api";
import Papa from "papaparse";
const Map = () => {
const [markers, setMarkers] = useState([]);
const mapRef = useRef(null);
const center = {
lat: 49.87099818,
lng: -97.28513314,
};
useEffect(() => {
const fetchData = async () => {
const response = await fetch("/data/geocoords.csv");
const data = await response.text();
const parsedData = Papa.parse(data, { header: true });
const initialMarkers = parsedData.data
.filter((data) => {
const lat = parseFloat(data.latitude);
const lng = parseFloat(data.longitude);
return mapRef.current.getBounds().contains({ lat, lng });
})
.map((data) => ({
position: {
lat: parseFloat(data.latitude),
lng: parseFloat(data.longitude),
},
title: data.title,
address: data.address,
}));
setMarkers(initialMarkers);
};
fetchData();
}, []);
const handleBoundsChanged = () => {
const map = mapRef.current;
const bounds = map.getBounds();
const ne = bounds.getNorthEast();
const sw = bounds.getSouthWest();
const lat1 = ne.lat();
const lng1 = ne.lng();
const lat2 = sw.lat();
const lng2 = sw.lng();
Papa.parse("/data/geocoords.csv", {
download: true,
header: true,
complete: (results) => {
const newMarkers = results.data
.filter((data) => {
const lat = parseFloat(data.latitude);
const lng = parseFloat(data.longitude);
return (
lat >= lat2 &&
lat <= lat1 &&
lng >= lng2 &&
lng <= lng1 &&
!markers.some((marker) => marker.street === data.street)
);
})
.map((data) => ({
position: {
lat: parseFloat(data.latitude),
lng: parseFloat(data.longitude),
},
title: data.street,
}));
setMarkers([...markers, ...newMarkers]);
},
});
};
return (
<LoadScript googleMapsApiKey={"my_api_key"}>
<GoogleMap
ref={mapRef}
onBoundsChanged={handleBoundsChanged}
mapContainerStyle={{ height: "100vh", width: "100%" }}
zoom={10}
center={center}
>
{markers.map((marker, index) => (
<Marker key={index} position={marker.position} title={marker.title} />
))}
</GoogleMap>
</LoadScript>
);
};
export default Map;
this is the screen shot of my error that i'm getting in google chrome console
Please any one can solve my problem i am stuck on this from last two days thanks in advance
Try to check whether the mapRef.current. has the getBounds() method before using it in the useEffect, try this in the useEffect return statement
return mapRef.current?.getBounds()?.contains({ lat, lng });
import React from 'react'
import { GoogleMap, Marker, useJsApiLoader } from '#react-google-maps/api';
const containerStyle = {
width: '95%',
height: '550px'
};
const center = {
lat: 27.8103,
lng: 70.4125
};
function Map(props) {
const { isLoaded } = useJsApiLoader({
id: 'google-map-script',
googleMapsApiKey: "API KEY"
})
const [map, setMap] = React.useState(null)
const onLoad = React.useCallback(function callback(map) {
const bounds = new window.google.maps.LatLngBounds({
lat: props.lat,
lng: props.lng
});
map.fitBounds(bounds);
setMap(map)
}, [])
const onUnmount = React.useCallback(function callback(map) {
setMap(null)
}, [])
return isLoaded ? (
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={20}
onLoad={onLoad}
onUnmount={onUnmount}>
<></>
<Marker onLoad={onLoad} position={center} />
</GoogleMap>
) : <></>
}
export default Map
I tried pinning the marker to the location but the map is now not showing as the marker is not showing. Totally blank page
After following this example provided by google for adding a map and markers to a react application using #googlemaps/react-wrapper, I decided to try adding marker clustering to my app. However, I am unable to get the clusters to appear.
I tried following this question/answer but no clusters have appeared despite adding new MarkerClusterer({ ref, markers }); to my code:
App.js
/*global google*/
import { useState, useEffect, useRef } from "react";
import { Wrapper } from "#googlemaps/react-wrapper";
import { MarkerClusterer } from "#googlemaps/markerclusterer";
import { createCustomEqual } from "fast-equals";
const markerList = [
{ id: "A12345", uuid: "500924cf83424aad9e7d386bbec88ef6", lat: 44.459744, lng: -73.214126, assetName: "A" },
{ id: "B09423", uuid: "500924cf83424aad9e7d386bbec88ef6", lat: 44.465291, lng: -73.190723, assetName: "A" },
{ id: "C98765", uuid: "c0385833-e483-40d1-803f-2b4c26ae3799", lat: 44.476949, lng: -73.210578, assetName: "B" },
{ id: "D99999", uuid: "c0385833-e483-40d1-803f-2b4c26ae3799", lat: 44.444572, lng: -73.208741, assetName: "B" },
{ id: "E12345", uuid: "500924cf83424aad9e7d386bbec88ef6", lat: 38.459744, lng: -81.214126, assetName: "A" },
{ id: "F09423", uuid: "500924cf83424aad9e7d386bbec88ef6", lat: 38.465291, lng: -81.190723, assetName: "A" },
{ id: "G98765", uuid: "c0385833-e483-40d1-803f-2b4c26ae3799", lat: 38.476949, lng: -81.210578, assetName: "B" },
{ id: "H99999", uuid: "c0385833-e483-40d1-803f-2b4c26ae3799", lat: 38.444572, lng: -81.208741, assetName: "B" },
]
const render = (status) => {
return <h1>{status}</h1>;
};
const App = () => {
const [zoom, setZoom] = useState(8); // initial zoom
const [center, setCenter] = useState({ lat: 44.45, lng: -73.21 });
const onIdle = (m) => {
console.log("onIdle");
};
return (
<>
<div style={{ width: "500px", height: "500px" }}>
<Wrapper
apiKey={process.env.REACT_APP_GOOGLE_MAPS_API_KEY}
render={render}
>
<Map
center={center}
onIdle={onIdle}
zoom={zoom}
style={{ flexGrow: "1", height: "100%" }}
/>
</Wrapper>
</div>
</>
);
};
const Map = ({ onIdle, children, style, ...options }) => {
const ref = useRef(null);
const [map, setMap] = useState();
useEffect(() => {
if (ref.current && !map) {
setMap(new window.google.maps.Map(ref.current, {}));
}
// Add some markers to the map.
const markers = markerList.map((m) => {
return new window.google.maps.Marker({
position: { lat: parseFloat(m.lat), lng: parseFloat(m.lng) }
//map: map,
});
});
// Add a marker clusterer to manage the markers.
new MarkerClusterer({ ref, markers });
}, [ref, map]);
useDeepCompareEffectForMaps(() => {
if (map) {
map.setOptions(options);
}
}, [map, options]);
useEffect(() => {
if (map) {
["click", "idle"].forEach((eventName) =>
google.maps.event.clearListeners(map, eventName)
);
if (onIdle) {
map.addListener("idle", () => onIdle(map));
}
}
}, [map, onIdle]);
return (
<>
<div ref={ref} style={style} />
</>
);
};
const deepCompareEqualsForMaps = createCustomEqual((deepEqual) => (a, b) => {
if (a instanceof google.maps.LatLng || b instanceof google.maps.LatLng) {
return new google.maps.LatLng(a).equals(new google.maps.LatLng(b));
}
return deepEqual(a, b);
});
function useDeepCompareMemoize(value) {
const ref = useRef();
if (!deepCompareEqualsForMaps(value, ref.current)) {
ref.current = value;
}
return ref.current;
}
function useDeepCompareEffectForMaps(callback, dependencies) {
useEffect(callback, dependencies.map(useDeepCompareMemoize));
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
How can I get MarkerClusterer to function with #googlemaps/react-wrapper?
Replace
new MarkerClusterer({ ref, markers });
with
new MarkerClusterer({ map, markers });
PS: The answer to the mentioned question was updated
I need to focus on the marker based on dynamic search results. As far as I understood from other discussions, what I need is to make a use of panTo function.
I gave a try, and it works partially.
Here is the code example and the codesandbox link
Note: Provide your Google Maps key in .env file to test the codesandbox.
Map.js
import { GoogleMap, LoadScript, Marker } from "#react-google-maps/api";
import React, { useEffect } from "react";
const containerStyle = {
width: "100%",
height: "500px"
};
const center = { lat: 45.4211, lng: -75.6903 };
const Maps = ({ filteredPark }) => {
const key = process.env.REACT_APP_GOOGLE_KEY; //test with your key please
if (!key) {
throw new Error("Google token is not set");
}
const mapRef = React.useRef();
const onMapLoad = React.useCallback((map) => {
mapRef.current = map;
}, []);
const panTo = React.useCallback(({ lat, lng }) => {
mapRef?.current?.panTo({ lat, lng });
mapRef?.current?.setZoom(18);
}, []);
useEffect(() => {
filteredPark.map((a) => {
const lat = a.coordinates[1];
const lng = a.coordinates[0];
return panTo({ lat, lng });
});
}, [filteredPark, panTo]);
return (
<div style={{ width: "100%", height: "100%" }}>
<LoadScript googleMapsApiKey={key}>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={6}
onLoad={onMapLoad}
>
{filteredPark &&
filteredPark.map((park) => (
<Marker
key={park.PARK_ID}
position={{
lat: park.coordinates[1],
lng: park.coordinates[0]
}}
/>
))}
</GoogleMap>
</LoadScript>
</div>
);
};
export default React.memo(Maps);
custom search hook component
import { useMemo } from "react";
import * as parkData from "./data/skateboard-parks.json";
const useParkFIlter = (DESCRIPT_1) => {
const gridRows = parkData.features.map((park) => park.properties);
const filteredParks = useMemo(() => {
return (
gridRows &&
gridRows.filter((dispo) =>
dispo.DESCRIPT_1.toLowerCase().includes(DESCRIPT_1.toLowerCase())
)
);
}, [DESCRIPT_1, gridRows]);
return [filteredParks];
};
export default useParkFIlter;
Any help will be appreciated.
I have made changes in your code. If it doesn't work, do let me know.
import { GoogleMap, LoadScript, Marker } from "#react-google-maps/api";
import React, { useEffect } from "react";
const containerStyle = {
width: "100%",
height: "500px"
};
const center = { lat: 45.4211, lng: -75.6903 };
const Maps = ({ filteredPark }) => {
const key = process.env.REACT_APP_GOOGLE_KEY; //test with your key please
if (!key) {
throw new Error("Google token is not set");
}
//const mapRef = React.useRef();
const [mapInstance, setMapInstance] = useState(null);
const[zoom, setZoom] = useState(6);
const onMapLoad = React.useCallback((map) => {
//mapRef.current = map;
setMapInstance(map);
}, []);
//const panTo = React.useCallback(({ lat, lng }) => {
//mapRef?.current?.panTo({ lat, lng });
//mapRef?.current?.setZoom(18);
//}, []);
useEffect(()=>{
if(mapInstance){
mapInstance.panTo({ lat, lng });
setZoom(18);
}
},[mapInstance]);
useEffect(() => {
filteredPark.map((a) => {
const lat = a.coordinates[1];
const lng = a.coordinates[0];
return panTo({ lat, lng });
});
}, [filteredPark, panTo]);
return (
<div style={{ width: "100%", height: "100%" }}>
<LoadScript googleMapsApiKey={key}>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
//zoom={6}
zoom={zoom}
onLoad={onMapLoad}
>
{filteredPark &&
filteredPark.map((park) => (
<Marker
key={park.PARK_ID}
position={{
lat: park.coordinates[1],
lng: park.coordinates[0]
}}
/>
))}
</GoogleMap>
</LoadScript>
</div>
);
};
export default React.memo(Maps);
I am using the Google Maps API and I want to limit the number of markers on the map (limit: 10). I couldn't find anything related to it in the API docs
neither I can find any similar source to solve my problem.
Here is my code:
import React from "react";
import {
GoogleMap,
useLoadScript,
Marker,
} from "#react-google-maps/api";
const mapContainerStyle = {
height: "50vh",
width: "100vw",
};
const options = {
zoomControl: false,
scrollwheel: false,
draggable: false
};
const center = {
lat: 34.155834,
lng: -119.202789,
};
export default function App() {
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: "AIzaSyCpaQDSgGTCetTR0uz42RyV80cByaGaYLs",
});
const [markers, setMarkers] = React.useState([]);
const onMapClick =
React.useCallback((e) => {
setMarkers((current) =>
[
...current,
{
lat: e.latLng.lat(),
lng: e.latLng.lng(),
},
]);
}, []);
const mapRef = React.useRef();
const onMapLoad = React.useCallback((map) => {
mapRef.current = map;
}, []);
if (loadError) return "Error";
if (!isLoaded) return "Loading...";
return (
<div>
<GoogleMap
id="map"
mapContainerStyle={mapContainerStyle}
zoom={14}
center={center}
options={options}
onClick={onMapClick}
onLoad={onMapLoad}
>
{markers.map((marker) => (
<Marker
key={`${marker.lat}-${marker.lng}`}
position={{ lat: marker.lat, lng: marker.lng }}
onClick={() => {
console.log("clicked")
}}
/>
))}
</GoogleMap>
</div>
);
}
How do I set the number of clicks up to 10?
You could do something like this. You may also want to call another function before returning current to perform some other update to alert the user they are maxed out on markers.
const onMapClick = React.useCallback((e) => {
setMarkers((current) => {
if (current.length < 10) {
return [
...current,
{
lat: e.latLng.lat(),
lng: e.latLng.lng()
}
];
} else {
return current;
};
});
}, []);