提问人:turtle 提问时间:3/22/2012 最后编辑:pkambturtle 更新时间:1/7/2020 访问量:73769
MKMapView:自定义视图,而不是注释固定
MKMapView: Instead of Annotation Pin, a custom view
问:
我想在我的而不是小石头别针中显示图像。MKMapView
有人可以在这里放一些有用的代码,或者告诉如何做吗?
-(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;
}
我希望我的图像粉红色 .jpg 出现在地图上,固定位置而不是默认的图钉视图(岩石针形状)。但是我仍然得到图钉的默认图像。
答:
119赞
user467105
3/22/2012
#1
如果要将自己的图像用于注释视图,则应创建一个 .MKAnnotationView
MKPinAnnotationView
MKPinAnnotationView
是 的子类,因此它有一个属性,但它通常会覆盖该属性并绘制一个图钉图像(这就是它的用途)。MKAnnotationView
image
因此,将代码更改为:
-(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
如果您仍然希望删除图像注释,则必须自己制作动画。您可以在 Stack Overflow 中搜索“animatesdrop mkannotationview”,您会找到几个答案。以下是前两个:
- 是否可以在 MKAnnotationView 而不是 MKPinAnnotationView 中调用 animatesDrop?
- 如何使用 MKAnnotationView 创建自定义的“pin-drop”动画?
评论
0赞
turtle
5/25/2012
嗨,安娜,我只想在单击此图钉图像时添加一个事件。我该怎么做?
0赞
casillas
12/16/2014
嗨,安娜,你对以下问题有什么想法吗 stackoverflow.com/questions/27489898/image-annotation-on-ios
0赞
Bhavin Ramani
2/26/2016
@Anna请帮我解决这个问题 stackoverflow.com/questions/35647839/......
2赞
Rob
12/17/2016
Anna,你的语句需要一个子句,否则重复使用的引脚将不会使用 .例如,.else
if
annotation
if ( pinView == nil ) { ... } else { pinView.annotation = annotation; }
19赞
Andrey Gordeev
7/2/2016
#2
这是关于 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
}
斯威夫特 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
}
评论
0赞
Andrej
4/29/2017
谢谢。你为什么打电话?我以为注释和视图是自动链接的?annotationView?.annotation = annotation
1赞
Andrey Gordeev
5/2/2017
它们不会自动链接。您正在取消对可重用批注视图的排队。如果它之前使用过,它可能有另一个链接。annotation
0赞
Dushko Cizaloski
3/23/2017
#3
我同意 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
}
评论
image
MKAnnotationView
UIImage