User location annotation misplaced when not default
I am showing the user location in an MKMapView. I don't want the default blue circle but instead the red pin.
Therefore I implemented mapView:viewForAnnotation: and made it always return a red pin as the user's location is the only pin I will show on the map view.
When using the default blue circle the location is quite precise but when changing to the red pin it's off quite a lot. It's as if the red pin is dropped precisely and is then moved up so it does not longer show my current position.
This is my code:
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSLog(@"MapView: Annotation.");
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
[pin setPinColor:MKPinAnnotationColorRed];
[pin setAnimatesDrop:YES];
[pin setCanShowCallout:NO];
return pin;
}
I use mapView:didAddAnnotationViews: to center the map view to my current location.
- (void)mapView:(MKMapView *)_mapView didAddAnnotationViews:(NSArray *)views
{
for(MKAnnotationView *annotationView in views)
{
if(annotationView.annotation == _mapView.userLocation)
{
MKCoordinateRegion region;
开发者_JAVA技巧 MKCoordinateSpan span;
span.latitudeDelta = 0.20;
span.longitudeDelta = 0.20;
CLLocationCoordinate2D location = [[_mapView userLocation] coordinate];
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
}
}
Does anyone know why my annotation is no longer placed correct when using the red pin instead of the default annotation view?
Setting [pin setAnimatesDrop:NO]
instead of YES
solved this.
精彩评论