注解方法 removeAnnotation 没有被调用,因此地图上的图像在更新位置时重复。如何调用 removeAnnotation 方法,以便我们可以从旧位置删除图像。
<pre>
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:@"Bike.png"];
annotationView.annotation = annotation;
return annotationView;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// here do
//UIImage *pinImage = [UIImage imageNamed:@"Bike.png"];
CLLocation *currentLocation = newLocation;
if(currentLocation != nil)
{
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"India" subTitle:@"Sarvopari Mall"];
if (newLocation)
{
[self.myMapView removeAnnotation:annotation];
}
[self.myMapView addAnnotation:annotation];
}
}
</pre>