我正在使用Mapbox API制作一个正在运行的应用程序,并且正在努力设置MGLMapView的边界以适合用户通过运行创建的所有折线。Mapbox的JavaScript版本如下:https://www.mapbox.com/mapbox-gl-js/example/zoomto-linestring/
但是我不知道如何使用swift来实现它。我找不到fitBounds等效项。
我将每个CLLocationCoordinate2D存储到一个名为pointsArray的数组中,并将其与mapView一起传递给结果视图控制器。
我有找到数组中两个点之间的最长距离并将其设置为边界的方法,但是由于算法为O(n ^ 2),因此如果我有太多点,则计算起来会花费很长时间。在其他情况下,也无法满足所有要求。
如果您想查看任何特定的代码,请告诉我。不知道要显示什么,因为我没有合适的范围。
…或者您可以改为这样做
mapView?.setVisibleCoordinates(
coordinates,
count: UInt(coordinates.count),
edgePadding: UIEdgeInsetsMake(20, 20, 20, 20),
animated: true
)
Run Code Online (Sandbox Code Playgroud)
小智 6
这是我的解决方案:
mapView.setVisibleCoordinateBounds(generateCoordinatesBounds(forCoordinates: [myCLLocationCoordinate2D]), animated: true)
func generateCoordinatesBounds(forCoordinates coordinates: [CLLocationCoordinate2D]) -> MGLCoordinateBounds {
var maxN = CLLocationDegrees(), maxS = CLLocationDegrees() , maxE = CLLocationDegrees() , maxW = CLLocationDegrees()
for coordinates in coordinates {
if coordinates.latitude >= maxN || maxN == 0 { maxN = coordinates.latitude }
if coordinates.latitude <= maxS || maxS == 0 { maxS = coordinates.latitude }
if coordinates.longitude >= maxE || maxE == 0 { maxE = coordinates.longitude }
if coordinates.longitude <= maxW || maxW == 0{ maxW = coordinates.longitude }
}
let offset = 0.001
let maxNE = CLLocationCoordinate2D(latitude: maxN + offset, longitude: maxE + offset)
let maxSW = CLLocationCoordinate2D(latitude: maxS - offset, longitude: maxW - offset)
return MGLCoordinateBounds(sw: maxSW, ne: maxNE)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |