1

We are building a mapkit iOS application where we are using custom annotations to put specific information about our school buildings on map pins. When the app and map first loads, everything works great. However, if we zoom, move, or rotate the map, the images inside the map annotations get mixed up and the wrong image appears on the pin. Coincidentally, the text always remains on the correct. We thought about reloading the annotations after the map movement has stopped, but we were curious if the Stack Overflow community had any other insights.

We are using Xcode version 13.4.1 with iOS 15.5. Below is our code for placing the annotations:

//here is the extension for the MKMapViewDelegate

extension ViewController: MKMapViewDelegate {//extension of the view controller that goes through each marker and then adds the annotation callout
  
  func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotation = annotation as? buildings else {//check is the marker is a buildings type
      return nil
    }
      
    let identifier = "buildings"//checks for each building item
    var view: MKMarkerAnnotationView
    if let dequeuedView = mapView.dequeueReusableAnnotationView(
      withIdentifier: identifier) as? MKMarkerAnnotationView {
      dequeuedView.annotation = annotation
      view = dequeuedView
    }
      else {
          view = MKMarkerAnnotationView(annotation: annotation,reuseIdentifier: identifier)//in each marker, place an annotation using the button type to the right of the text
          view.canShowCallout = true
          view.calloutOffset = CGPoint(x: -5, y: 5)
          var coolButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))//make the right side item be a button with a custom image
//          coolButton.setBackgroundImage(annotation.buildingImage, for: .normal)
          coolButton.setImage(annotation.buildingImage, for: .normal)
          view.rightCalloutAccessoryView = coolButton//set it here
    }
    return view
  }

//extension for the mapView

private extension MKMapView {//extends the map view to set center location coordinates
  func centerToLocation(_ location: CLLocation,regionRadius: CLLocationDistance = 200) {
    let coordinateRegion = MKCoordinateRegion(
      center: location.coordinate,
      latitudinalMeters: regionRadius,
      longitudinalMeters: regionRadius)
      setRegion(coordinateRegion, animated: true)
  }
}

//adding annotations

let footBallField = buildings(buildingName: "Football Field", buildingNums: "Football/Soccer Field", discipline: "sports", coordinate: CLLocationCoordinate2D(latitude: 40.899529240722316, longitude: -73.89879382455969), buildingImage: UIImage(named: "football"))
        let roseField = buildings(buildingName: "The Rose Field", buildingNums: "Lacrosse Field", discipline: "sports", coordinate: CLLocationCoordinate2D(latitude: 40.897499, longitude: -73.900656), buildingImage: UIImage(named: "rose"))

        //add annotations to map
        mapView.addAnnotation(footBallField)
        mapView.addAnnotation(roseField)
Drew
  • 11
  • 1

0 Answers0