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

时间:2014-11-26 19:44:08

标签: swift mapkit

我是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应用于地图?

8 个答案:

答案 0 :(得分:29)

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

答案 1 :(得分:6)

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

myMapView.setVisibleMapRect(zoomRect, animated: true)

答案 2 :(得分: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)
    }
}

答案 3 :(得分:3)

使用以下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)
        }
    }
}

答案 4 :(得分:0)

-[MKMapView showAnnotations:animated:]

答案 5 :(得分: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
}

布尔值仅用于确保一旦第一次更新,如果用户在地图上的其他位置导航,它就不会再次更新:)

答案 6 :(得分: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)

    }

答案 7 :(得分:0)

添加MKMapViewDelegate并在ViewDidLoad中连接mapView委托

mapView.delegate =. self


func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    let region = MKCoordinateRegion(center: userLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    self.mapView.setRegion(region, animated: true)
}