MKMapView:一个自定义视图,而不是 Annotation Pin [英] MKMapView: Instead of Annotation Pin, a custom view
问题描述
我想在我的 MKMapView
中显示一个图像,而不是小 rock pin.
I want to display an image in my MKMapView
instead of little rock pin.
有人可以在这里放一些有用的代码,或者告诉我怎么做吗?
Can someone please put some helpful code here, or tell the way how to do it?
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
我希望我的图像 pinks.jpg 出现在地图上,固定位置而不是默认的大头针视图(岩石大头针形状).但我仍然得到了引脚的默认图像.
I am expecting my image pinks.jpg to be on the map, pinning the location instead of default pin view (rock pin shaped). But still I am getting the default image of the pin.
推荐答案
当您想将自己的图像用于注释视图时,您应该创建一个 MKAnnotationView
而不是 MKPinAnnotationView代码>.
When you want to use your own image for an annotation view, you should create an MKAnnotationView
instead of an MKPinAnnotationView
.
MKPinAnnotationView
是 MKAnnotationView
的子类,所以它有一个 image
属性,但它通常会覆盖它并绘制一个图钉图像(这就是它的内容对于).
MKPinAnnotationView
is a subclass of MKAnnotationView
so it has an image
property but it generally overrides that and draws a pin image (that's what it's for).
因此将代码更改为:
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
//pinView.animatesDrop = YES;
pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch
}
else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
请注意,animatesDrop
也被注释掉了,因为该属性只存在于 MKPinAnnotationView
中.
Notice that animatesDrop
is also commented out since that property only exists in MKPinAnnotationView
.
如果您仍希望删除图像注释,则必须自己制作动画.您可以在 Stack Overflow 上搜索animatesdrop mkannotationview",您会找到几个答案.这是前两个:
If you still want your image annotations to drop, you'll have to do the animation yourself. You can search Stack Overflow for "animatesdrop mkannotationview" and you'll find several answers. Here are the first two:
- 是否可以调用animatesDrop 在 MKAnnotationView 而不是 MKPinAnnotationView 中?
- 如何创建自定义"针落"使用 MKAnnotationView 制作动画?
这篇关于MKMapView:一个自定义视图,而不是 Annotation Pin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!