SO关于在Google地图中合并javascript的多边形有一个很好的答案。现在,我把它转换成swift。这主要是有效的,但在某些圈子中遇到空白,所以我不确定转换是否完全正确。
转换后的JS的快速代码看起来像是
func setFloodPolygons(mData: Floods) {
let rect = GMSMutablePath()
var circCoordinates: [CLLocationCoordinate2D] = []
for flood in mData.Floods {
circCoordinates.append(contentsOf: drawPolygonCircle(coord: CLLocationCoordinate2D.init(latitude: flood.lat, longitude: flood.lng), radius: 3.0, dir: 1))
}
for coo in circCoordinates {
rect.add(coo)
}
// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red: 144/255, green: 195/255, blue: 212/255, alpha: 0.75)
polygon.strokeColor = UIColor(red: 144/255, green: 195/255, blue: 212/255, alpha: 0.75)
polygon.strokeWidth = 1
polygon.zIndex = 1
polygon.map = mapView
}
,圆圈功能
func drawPolygonCircle(coord: CLLocationCoordinate2D, radius: Double, dir: Int) -> [CLLocationCoordinate2D]{
let d2r = Double.pi / 180 // degrees to radians
let r2d = 180 / Double.pi // radians to degrees
let earthsradius = 3963.0 // 3963 is the radius of the earth in miles
let points = 32
let rlat = (radius / earthsradius) * r2d
let rlng = rlat / cos(coord.latitude * d2r)
var extp: [CLLocationCoordinate2D] = []
let start = 0
let end = points
for i in start...end {
let j = i + dir
let theta = Double.pi * (Double(j) / Double(points / 2));
let ey = coord.longitude + (rlng * cos(theta)); // center a + radius x * cos(theta)
let ex = coord.latitude + (rlat * sin(theta)); // center b + radius y * sin(theta)
extp.append(CLLocationCoordinate2D.init(latitude: ex, longitude: ey))
}
return extp;
}
当使用网页中相同的坐标完成相同的操作时,圆圈会正确显示。
我确定的间隙是多边形看起来不通过圆函数的情况。
任何人都可以看到可能需要调整以填补空白或了解我在转换中做错了什么?
更新:
这是Google公认的错误。您可以在https://issuetracker.google.com/issues/79475939查看错误报告。也许添加评论以鼓励Google尽快查看。