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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With