gpt4 book ai didi

ios - 带有 Google Maps iOS SDK 的 GEOSwift(绘制多边形)

转载 作者:行者123 更新时间:2023-11-28 11:27:34 25 4
gpt4 key购买 nike

我正在使用 GEOSwift 库 ( https://github.com/GEOSwift/GEOSwift )

我需要帮助:

1 - 如何在 Google map 上绘制多边形几何数据,代码如下:

let geometry = try! Geometry(wkt: "POLYGON((35 10, 45 45.5, 15 40, 10 20, 35 10))")

2 - 如何从几何 (geometry) 中以字符串或 CLLocationCoordiantes 的形式获取坐标(纬度、经度)

谢谢...

最佳答案

我假设您使用的是适用于 iOS 的 Google Maps SDK 版本 3.3.0 和 GEOSwift 版本 5.1.0。

您有一个表示为 WKT 的多边形(没有孔),并且您想在 Google map 上显示它。具体来说,您可能希望以 GMSPolygon 结束。

如果您知道您的 WKT 将始终是一个多边形,您实际上可以编写

let polygon = try! Polygon(wkt: "POLYGON((35 10, 45 45.5, 15 40, 10 20, 35 10))")

如果你不能保证,你可以按照你最初写的去做

let geometry = try! Geometry(wkt: "POLYGON((35 10, 45 45.5, 15 40, 10 20, 35 10))")

然后使用if case let/guard case let/switch case let提取多边形:

switch geometry {
case let .polygon(polygon):
// Do something with polygon
default:
// handle other types of Geometry or fail here
}

检查 definition of Geometry这显示了您可能关心处理的其他情况。

一旦你有了polygon: Polygon,你就可以得到代表它外部的点:

let points = polygon.exterior.points

Polygon.exterior 给你一个 Polygon.LinearRingPolygon.LinearRing.points 给你一个数组 Point.

现在您有了,您可以将它们映射到一个CLLocationCoordinate2D数组中

let coords = points.map { p in CLLocationCoordinate2D(latitude: p.y, longitude: p.x) }

请注意,y 与纬度相关,x 与经度相关。

现在您有了 coords,您可以使用其可变子类 GMSMutablePath 创建一个 GMSPath :

let path = GMSMutablePath()
for c in coords {
path.addCoordinate(c)
}

您可以使用该路径创建 GMSPolygon:

let polygon = GMSPolygon(path: path)

然后你只需要将它添加到你的 map 中:

polygon.map = yourMap

关于ios - 带有 Google Maps iOS SDK 的 GEOSwift(绘制多边形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com