如何在地图上添加 2 个不同的图钉? - Swift
How to add 2 different pins on Map? - Swift
我想在地图上使用两个不同的图钉,这是我的代码:
func mapView (_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
//avoid for user location
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "annId"
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if anView == nil {
if(annotation.subtitle! == "Offline"){
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.image = UIImage(named:"offIceCream.pdf")!
anView!.canShowCallout = true
}
if(annotation.subtitle! == "Online"){
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.image = UIImage(named:"onIceCream.pdf")!
anView!.canShowCallout = true
}
} else {
anView!.annotation = annotation
}
return anView
}
问题是它没有根据注释的副标题设置正确的图标。由于某种原因,有时它工作正常,有时它以相反的方式工作(在离线注释上设置在线图标,反之亦然)。知道为什么会这样吗?
提前致谢!
因为您忘记更新 .image
已经排队的注释视图:
if anView == nil {
...
}
else {
anView!.annotation = annotation
if (annotation.subtitle! == "Offline") {
anView!.image = UIImage(named:"offIceCream.pdf")!
}
else if (annotation.subtitle! == "Online") {
anView!.image = UIImage(named:"onIceCream.pdf")!
}
}
编写整个逻辑的更清晰的方式是:
func mapView (_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
if (annotation is MKUserLocation) {
return nil
}
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: "annId")
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: "annId")
}
else {
anView?.annotation = annotation
}
anView?.canShowCallout = true
if (annotation.subtitle! == "Offline") {
anView?.image = UIImage(named: "offIceCream.pdf")
}
else if (annotation.subtitle! == "Online") {
anView?.image = UIImage(named: "onIceCream.pdf")
}
return anView
}
我想在地图上使用两个不同的图钉,这是我的代码:
func mapView (_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
//avoid for user location
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "annId"
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if anView == nil {
if(annotation.subtitle! == "Offline"){
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.image = UIImage(named:"offIceCream.pdf")!
anView!.canShowCallout = true
}
if(annotation.subtitle! == "Online"){
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.image = UIImage(named:"onIceCream.pdf")!
anView!.canShowCallout = true
}
} else {
anView!.annotation = annotation
}
return anView
}
问题是它没有根据注释的副标题设置正确的图标。由于某种原因,有时它工作正常,有时它以相反的方式工作(在离线注释上设置在线图标,反之亦然)。知道为什么会这样吗?
提前致谢!
因为您忘记更新 .image
已经排队的注释视图:
if anView == nil {
...
}
else {
anView!.annotation = annotation
if (annotation.subtitle! == "Offline") {
anView!.image = UIImage(named:"offIceCream.pdf")!
}
else if (annotation.subtitle! == "Online") {
anView!.image = UIImage(named:"onIceCream.pdf")!
}
}
编写整个逻辑的更清晰的方式是:
func mapView (_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
if (annotation is MKUserLocation) {
return nil
}
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: "annId")
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: "annId")
}
else {
anView?.annotation = annotation
}
anView?.canShowCallout = true
if (annotation.subtitle! == "Offline") {
anView?.image = UIImage(named: "offIceCream.pdf")
}
else if (annotation.subtitle! == "Online") {
anView?.image = UIImage(named: "onIceCream.pdf")
}
return anView
}