用户位置气泡批注更改为自定义批注



我正在添加一个自定义注释以及用户的当前位置默认气泡注释,但用户位置注释在某个时间后会更改为其他自定义位置,而此时用户位置注释不在地图视图上。

我的viewForAnnotation方法是:

 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
 {
     NSString* annotationidentifier = @"customview";
     CustomAnnotationView *customannotationview = (CustomAnnotationView*) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationidentifier];
     if([annotation isKindOfClass:[MKUserLocation class]])
     {
         customannotationview = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationidentifier annotationviewimage:[UIImage imageNamed:@"location pin28.png"]];
         customannotationview.canShowCallout = YES;
         return customannotationview;

     }
     else if(![annotation isKindOfClass:[MKUserLocation class]] && annotation != _mapview.userLocation && (annotation.coordinate.latitude != _locationmanager.location.coordinate.latitude && annotation.coordinate.longitude != _locationmanager.location.coordinate.longitude))
     {
         customannotationview = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation reuseIdentifier:annotationidentifier annotationviewimage:[UIImage imageNamed:@"image1.png"]];
         return customannotationview;

     }
     return customannotationview;
 }

我已经在自定义注释中设置了条件,但过了一段时间,如果用户位置有一段时间不在焦点上,它仍然会更改为image1.png

我认为您的问题在于用户注释和位置注释都使用相同的标识符。结果是,当你滚动地图时,你的注释会被重用(就像表重用单元格一样),最终,你的一个位置注释会被用于你的用户注释。

试着给注释不同的标识符,或者,如果你有少量的位置,删除重用代码,这应该会修复它。

最新更新