ios MKMapView:一个自定义视图,而不是 Annotation Pin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9814988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MKMapView: Instead of Annotation Pin, a custom view
提问by turtle
I want to display an image in my MKMapView
instead of little rock pin.
我想在 myMKMapView
而不是 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;
}
I am expecting my image pinks.jpgto 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.
我希望我的图片pinks.jpg出现在地图上,固定位置而不是默认的大头针视图(岩石大头针形状)。但我仍然得到了引脚的默认图像。
回答by
When you want to use your own image for an annotation view, you should create an MKAnnotationView
instead of an MKPinAnnotationView
.
当您想将自己的图像用于注释视图时,您应该创建一个MKAnnotationView
而不是MKPinAnnotationView
.
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).
MKPinAnnotationView
是一个子类,MKAnnotationView
所以它有一个image
属性,但它通常会覆盖它并绘制一个图钉图像(这就是它的用途)。
So change the code to:
所以把代码改成:
-(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;
}
Notice that animatesDrop
is also commented out since that property only exists in MKPinAnnotationView
.
请注意,animatesDrop
也已注释掉,因为该属性仅存在于 中 MKPinAnnotationView
。
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:
如果您仍然希望删除图像注释,则必须自己制作动画。您可以在 Stack Overflow 上搜索“animatesdrop mkannotationview”,您会找到几个答案。这是前两个:
回答by Andrey Gordeev
Here's an answer on Swift 3. It dequeues annotation view if possible or creates a new one if not:
这是Swift 3的答案。如果可能,它将使注释视图出列,否则创建一个新视图:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
Swift 2.2:
斯威夫特 2.2:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
return nil
}
// Better to make this class property
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "yourImage")
}
return annotationView
}
回答by Dushko Cizaloski
I agree with with answer of the Anna and i like to show how will look that in swift3.This answer it's with many other options.Like a resize the image, get a list of images from array and ect.
我同意 Anna 的回答,我喜欢展示在swift3中的外观。这个答案有许多其他选项。比如调整图像大小,从数组中获取图像列表等等。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? PetrolStation {
let identifier = "pinAnnotation"
var view: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView { // 2
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 3
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
//here We put a coordinates where we like to show bubble with text information up on the pin image
view.calloutOffset = CGPoint(x: -7, y: 7)
//Here this is a array of images
let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace]
//Here we set the resize of the image
let size = CGSize(width: 30, height: 30)
UIGraphicsBeginImageContext(size)
pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
view.image = resizeImage
//Here we like to put into bubble window a singe for detail Informations
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
//Here we make change of standard pin image with our image
view.image = resizeImage
}
return view
}
return nil
}