Determine what the current delta / interval is
When the annotation is being processed, I focus this annotation on the map, no problem. The problem is, if the user has zoomed in or out on the original zoom level, I would like that level to be increased.
This is how I center the map. But then again, how do I get the current interval?
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
let span = MKCoordinateSpanMake(1.1, 1.1) //Get current span?
let region = MKCoordinateRegion(center: view.annotation.coordinate, span: span)
mapView.setRegion(region, animated: true)
}
+3
source to share
2 answers
You can simply query the current one MKMapView.region
and set a new center coordinate for it:
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
var region = mapView.region // get the current region
region.center = view.annotation.coordinate
mapView.setRegion(region, animated: true)
}
+3
source to share
To specifically define how to center around lat / lon while preserving the range, allowing the user to swing up and up, but always centering the point, here is some code I used. This means that a message is displayed on the screen when an action is performed on the map.
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
//Get the location that I am going to center on from a variable with apreviously set location.
//This is what I want to center on
let location = CLLocationCoordinate2D(
//getPropertyLocation.latitude/longitude already set to CLLocationDegrees in a prior process.
latitude: self.getPropertyLocation.latitude ,
longitude: self.getPropertyLocation.longitude
)
// Get the span that the mapView is set to by the user. "propertyMapView" is the MKMapView in this example.
let span = self.propertyMapView.region.span
// Now setup the region based on the lat/lon of the property and retain the span that already exists.
let region = MKCoordinateRegion(center: location, span: span)
//Center the view with some animation.
mapView.setRegion(region, animated: true)
}
0
source to share