【发布时间】:2021-03-04 14:37:16
【问题描述】:
我认为这两者是相关的,我可能在这里做错了:
首先我要覆盖 viewForAnnotation - 为了放置我的自定义图像
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// we avoid changing the user's location blue point
if([annotation isKindOfClass: [MKUserLocation class]]) {
return nil;
}
else
{
static NSString *SFAnnotationIdentifier = @"SFAnnotationIdentifier";
MKPinAnnotationView* pinAnnotationView = (MKPinAnnotationView*)[self.mapToPin dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinAnnotationView)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier];
UIImage *littleImage = [UIImage imageNamed:@"littleImage.png"];
// You may need to resize the image here.
annotationView.image = littleImage;
pinAnnotationView.draggable = true;
pinAnnotationView.canShowCallout = true;
pinAnnotationView.animatesDrop = true;
return annotationView;
}
else
{
pinAnnotationView.annotation = annotation;
}
return pinAnnotationView;
}
}
pinAnnotationView 的 draggable、canShowCallout 和 animatesDrop 似乎不起作用。
然后,我在 viewDidLoad 中调用我的方法来设置注释
-(void) setAnnotation:(NSString*)lat : (NSString*)lng
{
_mapAnnotation = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinateForPin;
coordinateForPin.latitude = [lat floatValue];
coordinateForPin.longitude = [lng floatValue];
[_mapAnnotation setCoordinate:coordinateForPin];
[_mapAnnotation setTitle:@"sometitle"];
[_mapToPin addAnnotation:_mapAnnotation];
}
似乎 [_mapAnnotation setTitle:@"sometitle"];当我覆盖 viewForAnnotation 时不再工作。
这是正确的方法吗?我一直在浏览,但没有找到任何可以提供帮助的解决方案。任何反馈将不胜感激。
【问题讨论】:
标签: ios mkannotationview mobile-development mkpinannotationview mkpointannotation