removeAnnotation方法未调用

时间:2013-09-04 08:50:27

标签: iphone ios mapkit

未调用注释方法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>

3 个答案:

答案 0 :(得分:2)

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

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

@interface YourViewController : UIViewController
{
    MyAnnotation *annotation;
}

@end

并修改您的LocationManager Delegate方法,如下所示:

- (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];
    }
}

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

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

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

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

我认为您应该使用 oldLocation 创建另一个注释,然后删除