如何在地图上添加两个不同的大头针?- - - - - -斯威夫特

时间:2022-01-13 21:20:26

I am trying to use two different pins on a map, this is the code I have:

我试着在地图上使用两个不同的大头针,这是我的代码:

    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
}

The problem is that it is not setting the correct icon depending on the subtitle of the annotation. For some reason sometimes it works correctly and sometimes it works the opposite way (sets the online icon on the offline annotations and viceversa). Any idea why this is happening?.

问题是它没有根据注释的副标题设置正确的图标。由于某些原因,有时它可以正常工作,有时它可以反过来工作(在离线注释和viceversa上设置在线图标)。你知道为什么会这样吗?

Thanks in advance!

提前谢谢!

1 个解决方案

#1


1  

Because you forget to update the .image of already queued annotation views:

因为您忘记更新已排队的注释视图的.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")!
  }
}

A clearer way of writing the entire logic would be:

更清楚地写出整个逻辑的方法是:

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
}

#1


1  

Because you forget to update the .image of already queued annotation views:

因为您忘记更新已排队的注释视图的.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")!
  }
}

A clearer way of writing the entire logic would be:

更清楚地写出整个逻辑的方法是:

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
}