I'm still a beginner in working with google maps and ReactJS. I'm using google-maps-react
to plot my map.
I have a property that is rendered on the map. This property is a set of several polygons, all of these polygons together form the property (farm).
When I click on a particular polygon, I get all the coordinates of that polygon that was clicked:
I would like to know, is it possible to show a 10km radius surrounded by the selected polygon?
From what I've researched, it is possible to show a radius through a single point, that is, a single latitude, and a single longitude. In my case, a polygon can have hundreds of latitutes and hundreds of longitutes. As you can see in the image above.
Can you tell me how do I configure a 10km radius based on the polygon that was selected?
I put my code into codesandbox
Here where I plot my map:
import React, { useState, useEffect } from "react";
import { Map, Polygon, GoogleApiWrapper } from "google-maps-react";
import data from "./api/coordinates.json";
const Colors = {
SELECTED: "blue",
DEFAULT: "#02482b",
OVER: "red"
};
const Maps = () => {
const [poligons, setPoligons] = useState([]);
const [selected, setSelected] = useState([]);
const [polygonOptions, setPolygonOptions] = useState({
strokeColor: Colors.DEFAULT,
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: Colors.DEFAULT,
fillOpacity: 0.35,
polygonKey: 1
});
const [areaPosition, setAreaPosition] = useState({
lat: -22.735840991240327,
lng: -47.574046945850164
});
const reduceMap = () => {
const _poligons = data.reduce((acc, [coords]) => {
const paths = coords.map(([lng, lat]) => ({ lng, lat }));
acc.push(paths);
return acc;
}, []);
setPoligons(_poligons);
};
useEffect(() => {
reduceMap();
}, []);
const selectedArea = (polygon) => {
for (let i = 0; i < selected.length; i++) {
const _polygon = selected[i];
_polygon.setOptions({ fillColor: Colors.DEFAULT });
}
setSelected([polygon]);
polygon.setOptions({ fillColor: Colors.SELECTED });
};
const handleClick = (props, polygon, event) => {
setAreaPosition({ lat: event.latLng.lat(), lng: event.latLng.lng() });
const paths = polygon.getPaths().getArray();
const coordinates = paths.map((path) => {
const points = path.getArray();
return points.map((point) => [point.lng(), point.lat()]);
});
selectedArea(polygon);
console.log("polygon selected: ", coordinates);
};
const handleMouseOver = (props, polygon) => {
polygon.setOptions({
fillColor:
polygon.fillColor !== Colors.SELECTED ? Colors.OVER : Colors.SELECTED
});
};
const handleMouseOut = (props, polygon) => {
polygon.setOptions({
fillColor:
polygon.fillColor !== Colors.SELECTED ? Colors.DEFAULT : Colors.SELECTED
});
};
return (
<>
<Map
google={google}
style={{ width: "90%", height: "70%", marginTop: "10px" }}
zoom={13}
initialCenter={{
lat: `${areaPosition.lat}`,
lng: `${areaPosition.lng}`
}}
clickableIcons={false}
className={"map"}
center={{ lat: `${areaPosition.lat}`, lng: `${areaPosition.lng}` }}
>
{poligons.map((coord, i) => (
<Polygon
key={`polygon-${i}`}
onMouseover={handleMouseOver}
onMouseout={handleMouseOut}
paths={coord}
options={polygonOptions}
onClick={handleClick}
/>
))}
</Map>
</>
);
};
export default GoogleApiWrapper({
apiKey: ""
})(Maps);
Thank you in advance.
Here's a code snippet of my function when clicking the polygon:
Here's the working code andd the whole code snippet: