Reputation: 13
I try to display a pin in a center of a map, unfortunatelly something is wrong and the pin shows up in the middle- right of the screen.
this is my code
// Single annotation
[geocoder geocodeAddressString:self.detailsAnnotation completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
self.latitudeOfPlaceFromTable = aPlacemark.location.coordinate.latitude;
self.longitudeOfPlaceFromTable = aPlacemark.location.coordinate.longitude;
MKCoordinateRegion viewRegion;
// 1 Center
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = self.latitudeOfPlaceFromTable;
zoomLocation.longitude = self.longitudeOfPlaceFromTable;
// Span
MKCoordinateSpan span;
span.latitudeDelta = 0.01f;
span.longitudeDelta = 0.01f;
// 2 Region
viewRegion.center = zoomLocation;
viewRegion.span = span;
// 3 Add an annotation
MKPointAnnotation * point = [[MKPointAnnotation alloc] init];
point.coordinate = zoomLocation;
point.title = self.titleAnnotation;
point.subtitle = self.detailsAnnotation;
[self.mapView addAnnotation:point];
// 4 Center the map on pin
[self.mapView setRegion:viewRegion animated:YES];
}
}];
Can someone gives some help ? Thanks
Upvotes: 1
Views: 430
Reputation: 13302
Try to change your last line of code with this one:
[self.mapView setRegion:[self.mapView regionThatFits:viewRegion] animated:YES];
Usually MapView can't display exact region that you try to set, because map isn't flat.
From Apple documentation on regionThatFits
method:
Adjusts the aspect ratio of the specified region to ensure that it fits in the map view’s frame. You can use this method to normalize the region values before displaying them in the map. This method returns a new region that both contains the specified region and fits neatly inside the map view’s frame.
Update: Another suggestion: be sure that map view width isn't more then actual screen width. In other case map view frame can overstep the right bound of screen that will results in wrong center point.
Upvotes: 2