【发布时间】:2017-01-04 17:04:54
【问题描述】:
我对 mapkit 有点陌生,但我想知道,我可以为地图上的每个图钉设置不同的图像吗?例如,字典中有用户信息,而不是常规的 pin 图像,必须有他们自己的图像。我应该如何为以下输出设置 viewFor 注释方法。
[{
email = "user1@gmail.com";
id = jqvDgcBoV9Y4sx1BHCmir5k90dr1;
name = User1;
profileImageUrl = "<null>";
}, {
email = "user2@gmail.com";
id = bqvDmcBoV9Y4sx1BqCmirnk90drz;
name = User2;
profileImageUrl = "https://firebasestorage.googleapis.com/v0/";
}, {
email = "user3@gmail.com";
id = axmDgcB5V9m4sx1nHC5ir5kn1dn3;
name = User3;
profileImageUrl = "https://firebasestorage.googleapis.com/v0/";
}]
顺便说一句,我有一个函数可以将 URL 转换为 UIImageView,但不是 UIImage,这是我最大的困难之一。
我的 viewForAnnotation 代表现在。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView: MKAnnotationView?
var annotationViewx: MKAnnotationView?
let annotationIdentifier = "AnnotationIdentifier"
guard !annotation.isKind(of: MKUserLocation.self) else {
var annotationViewq: MKAnnotationView?
annotationViewq = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
annotationViewq?.image = UIImage(named: "myLocation.png")
let size = CGSize(width: 17, height: 17)
UIGraphicsBeginImageContext(size)
annotationViewq?.image!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
annotationViewq?.image = resizedImage
annotationViewq?.isEnabled = true
annotationViewq?.isUserInteractionEnabled = true
return annotationViewq
}
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
annotationView?.canShowCallout = true
annotationView?.image = UIImage(named: "emptyPhoto.png")
let size = CGSize(width: 17, height: 17)
UIGraphicsBeginImageContext(size)
annotationView?.image!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
annotationView?.image = resizedImage
return annotationView
}
//This annotation not working. but not problem
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationViewx?.canShowCallout = true
annotationViewx?.image = UIImage(named: "trafficIcon.png")
let size = CGSize(width: 17, height: 17)
UIGraphicsBeginImageContext(size)
annotationViewx?.image!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
annotationViewx?.image = resizedImage
annotationViewx = av
return annotationViewx
}
【问题讨论】:
-
顺便说一句,你现在如何分配 pin 图像?
-
@Rob 先生,我没有看到标准 pin,我的 pin 就像我在代码中设置的一样,唯一不同的是我的位置 pin,但没有标准的 pin。我想为每个 pin 设置 profileImageUrl ..
-
我为第一条评论写了
viewFor Annotation方法,他想知道我如何在viewFor Annotation中分配图像。正如你所说,这是一个好的开始 Matt 的代码。当我处理这个问题时,我会编辑帖子,可能在问题解决后会更清楚:) 顺便说一句,英语不是我的第一语言,抱歉答案不清楚。我很抱歉占用您的时间。
标签: ios swift mapkit nsdictionary mkannotation