I'm using react.js and I currently know how to put a hole in a polygon via the following:
我正在使用react.js,目前我知道如何通过以下方法在多边形中放置一个洞:
drawShape(google){
var entireMap = [
new google.maps.LatLng(-85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, -180),
new google.maps.LatLng(85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 180),
new google.maps.LatLng(-85.1054596961173, 0)];
var innerCoords = [
{lat: 28.745, lng: -70.579},
{lat: 29.570, lng: -67.514},
{lat: 27.339, lng: -66.668}
];
const poly = new google.maps.Polygon({
paths: [entireMap, innerCoords],
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#000000',
fillOpacity: 0.35
});
poly.setMap(google.map);
}
Right now, I have a list of properties that contain lng and lat. Instead of having polygon as holes I want to have circles as holes, using the lng and lat.
现在,我有一个包含LNG和更高版本的属性列表。我不想让多边形作为洞,而是想用圆作为洞,使用LNG和LAT。
So far, I see that the documentation allows you to create circles separately:
到目前为止,我看到文档允许您单独创建圆:
var cityCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: citymap[city].center,
radius: Math.sqrt(citymap[city].population) * 100
});
However, I would like the circles to be "holes" within the entireMap
polygon. Is it possible to combine the entireMap
shape and circle shape or would I need to create a function that creates circles from the polygon shape?
然而,我希望这些圆圈是整个地图多边形内的“洞”。是否可以将整个映射形状和圆形状组合在一起,或者我是否需要创建一个从多边形形状创建圆的函数?
更多回答
优秀答案推荐
The following is the component I'm using to draw a polygon in map with a cut out circle in it. It's written with React in mind but the solution can be extracted for other libs/frameworks
下面是我用来在地图上绘制一个多边形的组件,里面有一个切出的圆圈。它在编写时考虑到了Reaction,但是可以为其他库/框架提取解决方案
import { useEffect } from 'react'
import { useGoogleMap } from '@react-google-maps/api'
import { Map as MapConsts } from 'assets/consts'
const getCirclePath = (
center: google.maps.LatLng | google.maps.LatLngLiteral,
radius: number,
numParts = 512
) => {
const path = []
for (let i = 0; i < numParts; i += 1) {
path.push(
google.maps.geometry.spherical.computeOffset(
center,
radius,
(i * 360) / numParts
)
)
}
return path
}
type Props = {
center: google.maps.LatLng | google.maps.LatLngLiteral,
radius: number
}
function SearchRadius({ center, radius }) {
const map = useGoogleMap()
useEffect(() => {
// calculate inner circle path
const innerCircle = getCirclePath(center, radius)
const polygon = new google.maps.Polygon({
paths: [MapConsts.OUTER_BOUNDS, innerCircle],
strokeColor: '#000000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#000000',
fillOpacity: 0.35,
map,
})
return () => {
// remove polygon from map
polygon.setMap(null)
}
}, [center, radius])
return null
}
export default SearchRadius
This solution requires the geometry library, to add that you can check this question here
此解决方案需要几何库,以添加您可以在此处检查此问题
更多回答
我是一名优秀的程序员,十分优秀!