简体   繁体   English

removeAnnotation方法未调用

[英]removeAnnotation method not called

Annotation method removeAnnotation is not called, so that the images on map duplicated while update location. 未调用注释方法removeAnnotation,以便在更新位置时复制地图上的图像。 How to call removeAnnotation method so that we can remove image from old location. 如何调用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>

You should keep the annotation as an instance variable or property while adding it to map. 在将注释添加到map时,应将注释保留为实例变量或属性。 Then while location updates, remove the previous annotation(stored in instance variable) and add new. 然后在位置更新时,删除先前的注释(存储在实例变量中)并添加新的注释。

You should modify your UIViewController like this: 你应该像这样修改你的UIViewController:

@interface YourViewController : UIViewController
{
    MyAnnotation *annotation;
}

@end

And modify your LocationManager Delegate methods like this: 并修改您的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];
    }
}
- (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;
    }

From looking at the code above, probably [self.myMapView removeAnnotation:annotation]; 从查看上面的代码,可能是[self.myMapView removeAnnotation:annotation]; will be called because if (newLocation) will be true as you are updating to a new location. 将被调用,因为if (newLocation)您更新到新位置时, if (newLocation)将为true。

Otherwise, in the code below you are removing and adding exactly the same annotation as you can see. 否则,在下面的代码中,您将删除并添加完全相同的注释。

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

I think you should instead create another annotation with the oldLocation and remove that one instead. 我认为您应该使用oldLocation 创建另一个注释而是 删除注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM