0

注解方法 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>
4

3 回答 3

2

在将注释添加到映射时,您应该将注释保留为实例变量或属性。然后在位置更新时,删除以前的注释(存储在实例变量中)并添加新的。

你应该像这样修改你的 UIViewController:

@interface YourViewController : UIViewController
{
    MyAnnotation *annotation;
}

@end

并像这样修改您的 LocationManager 委托方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    // here do
    //UIImage *pinImage = [UIImage imageNamed:@"Bike.png"];
    CLLocation *currentLocation = newLocation;
    if(currentLocation != nil)
    {
        //Removes previous annotation
        if(annotation){
            [self.myMapView removeAnnonation:annotation];
        }
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
        //Keeps annotation in an instance variable
        annotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"India" subTitle:@"Sarvopari Mall"];
        [self.myMapView addAnnotation:annotation];
    }
}
于 2013-09-04T09:58:40.913 回答
0
- (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];
        }

        [self.myMapView removeAnnotation:annotation];
        annotationView.image = [UIImage imageNamed:@"Bike.png"];
        annotationView.annotation = annotation;

        return annotationView;
    }
于 2013-09-04T08:59:38.957 回答
0

通过查看上面的代码,可能[self.myMapView removeAnnotation:annotation];会被调用,因为if (newLocation)当您更新到新位置时将是真的。

否则,在下面的代码中,您将删除和添加与您看到的完全相同的注释。

    if (newLocation)
    {
        [self.myMapView removeAnnotation:annotation];
    }
    [self.myMapView addAnnotation:annotation];

我认为您应该改为使用oldLocation创建另一个注释删除 该注释。

于 2013-09-04T09:03:18.283 回答