use*_*631 2 annotations mapkit mkmapview ios
认为使用我自己的自定义图钉进行注释会非常容易.
但我从来没有能够让它工作,我不明白为什么!
我只是使用:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"CustomViewAnnotation";
CustomAnnotationView * customAnnotationView = (CustomAnnotationView *) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(!customAnnotationView)
{
customAnnotationView=[[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
reuseIdentifier:annotationIdentifier
annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
}
customAnnotationView.image=[UIImage imageNamed:@"map_location_pin.png"];
customAnnotationView.canShowCallout= YES;
return customAnnotationView;
}
Run Code Online (Sandbox Code Playgroud)
我认为这应该有效,因为UIImage是它想要的东西,而且我的项目中确实有.png文件.
它从不使用我的图像,只是标准的红色图钉.我在这做错了什么?
几点想法:
你定义delegate为你的MKMapView?你有没有断点或NSLog在这里确保你viewForAnnotation的电话被叫?
您还没有共享您的CustomAnnotationView接口/实现细节,但MKAnnotationView除非您在那里做一些特殊的事情,否则您不需要子类.如果您只是替换图像,您可以这样做:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSString *annotationIdentifier = @"CustomViewAnnotation";
MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (annotationView) {
annotationView.annotation = annotation;
} else {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
}
annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
annotationView.canShowCallout= YES;
return annotationView;
}
Run Code Online (Sandbox Code Playgroud)
显然,如果你正在做一些有趣的事情(比如拖动或删除引脚时的自定义注释),那么继续使用你的CustomAnnotationView子类MKAnnotationView.这取决于你想要做什么.
与您原来的问题无关,我建议:
您的viewForAnnotation方法只使用局部mapView变量而不是引用类属性self.mapView(虽然它们无疑是相同的),
您考虑重命名自定义init方法.所以代替:
customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
reuseIdentifier:annotationIdentifier
annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
Run Code Online (Sandbox Code Playgroud)
你做了类似的事情:
customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier
image:[UIImage imageNamed:@"map_location_pin.png"]];
Run Code Online (Sandbox Code Playgroud)如果您仍然遇到麻烦,请CustomAnnotationView与我们分享您的部分代码.
| 归档时间: |
|
| 查看次数: |
4299 次 |
| 最近记录: |