gpt4 book ai didi

iphone - 未调用 removeAnnotation 方法

转载 作者:行者123 更新时间:2023-11-28 22:27:07 32 4
gpt4 key购买 nike

注释方法 removeAnnotation 没有被调用,所以 map 上的图像在更新位置时会被复制。如何调用 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>

最佳答案

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

您应该像这样修改您的 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];
}
}

关于iphone - 未调用 removeAnnotation 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609324/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com