-->

Zoom to fit current location and annotation on map

2020-08-11 11:27发布

问题:

I am new to swift (iOS programming in general) and am trying to figure out how to zoom a map out to fit 2 points on the map.

Currently I have

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);

Which does nothing.

Do I have to apply zoomRect to the map somehow?

回答1:

MKMapRectUnion computes and returns a new rect, nothing more. You need to tell the mapView to set its visible area to that new rect:

myMapView.setVisibleMapRect(zoomRect, animated: true)


回答2:

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


回答3:

Use below MKMapView extension-

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:

For swift 3, iOS 10

Get current location first, and using call back didUpdateLocations to configure the map. Add the following code in your view controller:

@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)
    }
}


回答5:

-[MKMapView showAnnotations:animated:]



回答6:

As @lveselovsky said

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

works perfectly.

If you are updating your UI before the map has loaded, you can put it in the delegate method like so:

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

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

Boolean value only to make sure once it's updated the first time, it does not get updated again if the user navigates somewhere else on the map :)



回答7:

Swift 3 Fit all annotations in map. It's the correct way.

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)

    }


回答8:

Add MKMapViewDelegate and connect mapView delegate in ViewDidLoad

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)
}


标签: swift mapkit