Удалить аннотацию с карты - Swift - PullRequest
0 голосов
/ 28 июня 2019

У меня проблемы. Я создал собственный класс для моей MKPointAnnotation, который содержит «идентификатор».

Теперь я хочу сравнить идентификатор, и если оба идентификатора совпадают, я хочу удалить аннотацию.

На данный момент у меня есть этот код, который проверяет, являются ли annotation.title и user.username одинаковыми, но я хочу изменить его с помощью: annotation.identifier и user.id.

 for annotation in self.mapView.annotations {
                    if let title = annotation.title, title == user.username {
                        self.mapView.removeAnnotation(annotation)
                    }

                }

Что касается пользовательского класса аннотации:

   class MyAnnotation: MKPointAnnotation {

    var identifier: String!
}

А для создания аннотации:

let annotation = MyAnnotation()
                    annotation.title = user.username
                    annotation.subtitle = user.job
                    annotation.identifier = user.email
                    annotation.coordinate = CLLocationCoordinate2D(latitude: (Double(user.latitude ?? "0"))!, longitude: (Double(user.longitude ?? "0"))!)

                    self.mapView.addAnnotation(annotation)

1 Ответ

1 голос
/ 29 июня 2019

Перед выполнением кода вам придется привести аннотацию к MyAnnotation.Попробуйте что-то вроде этого:


 for annotation in self.mapView.annotations {
      if let annotation = annotation as? MyAnnotation, annotation.identifier == user.id {
          self.mapView.removeAnnotation(annotation)
      }
 }

Надеюсь, это поможет.

...