缩放以适合地图上的当前位置和注释

问题描述 投票:12回答:7

我是swift(一般的iOS编程)的新手,我正在试图弄清楚如何缩放地图以适应地图上的2个点。

目前我有

var zoomRect = MKMapRectNull;
var myLocationPointRect = MKMapRectMake(myLocation.longitude, myLocation.latitude, 0, 0)
var currentDestinationPointRect = MKMapRectMake(currentDestination.longitude, currentDestination.latitude, 0, 0)

zoomRect = myLocationPointRect;
zoomRect = MKMapRectUnion(zoomRect, currentDestinationPointRect);

什么也没做。

我是否必须以某种方式将zoomRect应用于地图?

swift mapkit
7个回答
6
投票

MKMapRectUnion计算并返回一个新的rect,仅此而已。您需要告诉mapView将其可见区域设置为新的rect:

myMapView.setVisibleMapRect(zoomRect, animated: true)

29
投票
self.mapView.showAnnotations(self.mapView.annotations, animated: true)

3
投票

对于swift 3,iOS 10

首先获取当前位置,然后使用回调didUpdateLocations配置地图。在视图控制器中添加以下代码:

@IBOutlet weak var mapView: MKMapView!

private var locationManager: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    mapView.delegate = self

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    if CLLocationManager.locationServicesEnabled() {
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
}



public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let last = newLocation {
        let userlongitude = last.coordinate.longitude
        let userlatitude = last.coordinate.latitude
        let newDistance = CLLocation(latitude: userlatitude, longitude: userlongitude).distance(from: CLLocation(latitude: YourAnnotation.coordinate.latitude, longitude: YourAnnotation.coordinate.longitude))
        let region = MKCoordinateRegionMakeWithDistance(last.coordinate, 2 * newDistance, 2 * newDistance)
        let adjustRegion = self.mapView.regionThatFits(region)
        self.mapView.setRegion(adjustRegion, animated:true)
    }
}

2
投票

使用以下MKMapView扩展 -

extension MKMapView
{
    func fitAllMarkers(shouldIncludeCurrentLocation: Bool) {

        if !shouldIncludeCurrentLocation
        {
            showAnnotations(annotations, animated: true)
        }
        else
        {
            var zoomRect = MKMapRectNull

            let point = MKMapPointForCoordinate(userLocation.coordinate)
            let pointRect = MKMapRectMake(point.x, point.y, 0, 0)
            zoomRect = MKMapRectUnion(zoomRect, pointRect)

            for annotation in annotations {

                let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)
                let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0)

                if (MKMapRectIsNull(zoomRect)) {
                    zoomRect = pointRect
                } else {
                    zoomRect = MKMapRectUnion(zoomRect, pointRect)
                }
            }

            setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(8, 8, 8, 8), animated: true)
        }
    }
}

0
投票

-[MKMapView showAnnotations:animated:]


0
投票

正如@lveselovsky所说

self.mapView.showAnnotations(self.mapView.annotations, animated: true)

工作得很好。

如果要在加载地图之前更新UI,可以将其放在委托方法中,如下所示:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    guard !mapZoomUpdatedOnce else {
        return
    }

    self.mapView.showAnnotations(self.mapView.annotations, animated: true)
    self.mapZoomUpdatedOnce = true
}

布尔值只是为了确保一旦它第一次更新,如果用户导航到地图上的其他地方,它就不会再次更新:)


0
投票

Swift 3在地图中填入所有注释。这是正确的方法。

func zoomMapaFitAnnotations() {

        var zoomRect = MKMapRectNull
        for annotation in mapview.annotations {

            let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)

            let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0)

            if (MKMapRectIsNull(zoomRect)) {
                zoomRect = pointRect
            } else {
                zoomRect = MKMapRectUnion(zoomRect, pointRect)
            }
        }
        self.mapview.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true)

    }
© www.soinside.com 2019 - 2024. All rights reserved.