如何使用Swift将Mapbox缩放到以千米为单位的给定半径? [英] How to zoom Mapbox using Swift to a given radius in kilometers?

查看:68
本文介绍了如何使用Swift将Mapbox缩放到以千米为单位的给定半径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从旧版Mapbox SDK(1.6版)更新到了最新的Mapbox iOS SDK 3.x.

I recently updated from legacy Mapbox SDK (version 1.6) to latest Mapbox iOS SDK 3.x.

在新版本中,我无法弄清楚如何将Mapbox MGLMapView缩放到以千米为单位的给定半径以适合屏幕.

In new version I am not able to figure out how to zoom Mapbox MGLMapView to a given radius in kilometers to fit on screen.

在旧版本(1.6版)中,RMMapView.zoomWithLatitudeLongitudeBoundsSouthWest方法的工作如下:

In old (version 1.6) RMMapView.zoomWithLatitudeLongitudeBoundsSouthWest method does the job as following:

func centerAndZoom(center: CLLocationCoordinate2D, kilometers: Float) {
    let meters = Double(kilometers * 1000)
    let region = MKCoordinateRegionMakeWithDistance(center, meters / 2, meters / 2);

    let northEastLat = center.latitude - (region.span.latitudeDelta / 2);
    let northEastLon = center.longitude + (region.span.longitudeDelta / 2);
    let northEast = CLLocationCoordinate2D(latitude: northEastLat, longitude: northEastLon)

    let southEastLat = center.latitude + (region.span.latitudeDelta  / 2);
    let southEastLon = center.longitude - (region.span.longitudeDelta / 2);
    let southEast = CLLocationCoordinate2D(latitude: southEastLat, longitude: southEastLon)

    self.mapView.zoomWithLatitudeLongitudeBoundsSouthWest(southEast, northEast: northEast, animated: true)
}

如何使用Swift在最新的Mapbox中实现半径缩放?

How to achieve a radius zoom in latest Mapbox using Swift?

推荐答案

现在-setVisibleCoordinateBounds:animated将完成此工作:

更改接收器的视口以适合给定的坐标范围,并选择使更改生效.

Changes the receiver’s viewport to fit the given coordinate bounds, optionally animating the change.

Mapbox iOS SDK参考

这里是一个例子:

let bounds = MGLCoordinateBounds(
        sw: CLLocationCoordinate2D(latitude: 43.7115, longitude: 10.3725),
        ne: CLLocationCoordinate2D(latitude: 43.7318, longitude: 10.4222))
mapView.setVisibleCoordinateBounds(bounds, animated: false)

这篇关于如何使用Swift将Mapbox缩放到以千米为单位的给定半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆