Making the map zoom to user location and annotation (swift 2)

北慕城南 提交于 2019-11-29 18:50:29

问题


I am working with mapkit. I want the map to zoom to show the user's location and the annotated point, instead of just zooming to the user's current location.

Currently I have:

                            let annotation = MKPointAnnotation()
                            annotation.coordinate = CLLocationCoordinate2DMake(mapLat, mapLon)
                            annotation.title = mapTitle
                            self.map.addAnnotation(annotation)

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLoction: CLLocation = locations[0]
    let latitude = userLoction.coordinate.latitude
    let longitude = userLoction.coordinate.longitude
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    self.map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
}

It just zooms to the user's location and it gets locked on the location. When I try to scroll, it just jumps right back to the user's location. What am I doing wrong? How do I allow the user to zoom or move on the map without it jumping back to the current location?

I’m trying to zoom in to show the annotation and the user's location on the same view. Even if the annotation is far away, I want it to zoom to show both the user and annotation.


回答1:


It just jumps right back to the user's location, because didUpdateLocations method is called many times. There are two solutions.

1) Use requestLocation

If you use requestLocation method instead of startUpdatingLocation, didUpdateLocations method is called only once

if #available(iOS 9.0, *) {
    locationManager.requestLocation()
} else {
    // Fallback on earlier versions
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLoction: CLLocation = locations[0]
    let latitude = userLoction.coordinate.latitude
    let longitude = userLoction.coordinate.longitude
    let latDelta: CLLocationDegrees = 0.05
    let lonDelta: CLLocationDegrees = 0.05
    let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    self.map.setRegion(region, animated: true)
    self.map.showsUserLocation = true
}

2) Use flag

var isInitialized = false

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if !isInitialized {
        // Here is called only once
        isInitialized = true

        let userLoction: CLLocation = locations[0]
        let latitude = userLoction.coordinate.latitude
        let longitude = userLoction.coordinate.longitude
        let latDelta: CLLocationDegrees = 0.05
        let lonDelta: CLLocationDegrees = 0.05
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        self.map.setRegion(region, animated: true)
        self.map.showsUserLocation = true
    }
}


来源:https://stackoverflow.com/questions/33044148/making-the-map-zoom-to-user-location-and-annotation-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!