I have code to display pin annotations on MapView customized with left, right accessory views. There are three pin annotations and I pass them to the MapView via Binding locationPins, an array of structs. The three Pins are getting displayed with annotation text, But I it not working as expected for the following reasons:
-
The annotation text captured by label called subtitleView shows the value from the last pin annotation for all the three pins as: “2 Level 1: 120 V”, instead of displaying different values for pin annotations ( please see debug output below)
-
Debug shows the sequence of methods called. The mapView delegate gets called 3 times. When called each time it is supposed to return with one pin annotation. I know my logic is wrong since I loop inside the mapView delegate three times, whereas ideally I should be looping outside to call mapview delegate and pass one element of the array locationPins. So how to call the delegate and pass only one element of the array locationPins?
-
Also when mapview displays zooms in on only one pin. I have to physically pan to zoom out and then I see all the three pins. Why is not adjusting the map to show all three pins.
I need help?
Code:struct ProfileView: View { @Environment(.managedObjectContext) private var viewContext @Environment(.dismiss) var dismiss @State var tracking:MapUserTrackingMode = .follow @Environment(.presentationMode) var presentationMode @Binding var locationPins: [GpsData.MyEndPt] var body: some View { TabView { NavigationView { ZStack { VStack (alignment: .leading){ MapView(locationPins: $locationPins) .ignoresSafeArea() } } .safeAreaInset(edge: .bottom) { Color.clear .frame(height: 0) .background(.white) } .toolbar { ToolbarItem(placement: .principal) { // <3> VStack { Text("Profiles").font(.headline) } } } } } } struct MapView: UIViewRepresentable { @Binding var locationPins: [GpsData.MyEndPt] func makeUIView(context: Context) -> MKMapView { let view = MKMapView() view.delegate = context.coordinator return view } func updateUIView(_ uiView: MKMapView, context: Context) { print("updateUIView called") uiView.mapType = .mutedStandard uiView.addAnnotations(self.locationPins) } func makeCoordinator() -> MapViewDelegate{ print("coordinator called") var del = MapViewDelegate() del.addEndPoints(endPoints: locationPins) return del } class MapViewDelegate: MKMapView, MKMapViewDelegate { let PINID:String = "MyEndPoint" var mylocationPins: [GpsData.MyEndPt] = [] @State var locationId: String? @State var providerType: String? public func addEndPoints(endPoints: [GpsData.MyEndPt]) { print("addEndPoints called") self.mylocationPins = endPoints self.showAnnotations(endPoints, animated: true) } internal func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { print("mapview delegate called") var mPin: MKMarkerAnnotationView? = nil var mPins = [MKMarkerAnnotationView]() let subtitleView = UILabel() subtitleView.font = subtitleView.font.withSize(10) subtitleView.numberOfLines = 4 if (mPin == nil ) { mPin = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: PINID) mPin?.canShowCallout = true } else{ mPin?.annotation = annotation } mPin?.leftCalloutAccessoryView = nil mPin?.rightCalloutAccessoryView = nil if let myEndpt = annotation as? GpsData.MyEndPt { if myEndpt.thumbnailUrl != nil { mPin?.leftCalloutAccessoryView = UIButton(frame: CGRect(x: 0, y: 0, width: 55, height: 55) ) } mPin?.rightCalloutAccessoryView = UIButton(type: UIButton.ButtonType.detailDisclosure) var i: Int = 0 for pin in self.mylocationPins { subtitleView.text = "count: " subtitleView.text! += String(i) i += 1 print("subtitleView: (subtitleView.text)") } } return mPin! } //MARK:- delegate method func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { DispatchQueue.main.async { if let thumbnailImageButton = view.leftCalloutAccessoryView as? UIButton, let url = (view.annotation as? GpsData.MyEndPt)?.thumbnailUrl { print("URL: (url)") do{ let imageData: Data = try Data(contentsOf: url as URL) let image = UIImage(data: imageData) thumbnailImageButton.setImage(image, for: UIControl.State.normal) }catch{ print("Unable to load image data: (error)") } } } } } } }
Debug:
coordinator called
addEndPoints called
updateUIView called
mapview delegate called
subtitleView: Optional("0 rnLevel 2: 208-240V")
subtitleView: Optional("1 rnNo EV chargers")
subtitleView: Optional("2 rnLevel 1: 120 V")
mapview delegate called
subtitleView: Optional("0 rnLevel 2: 208-240V")
subtitleView: Optional("1 rnNo EV chargers")
subtitleView: Optional("2 rnLevel 1: 120 V")
mapview delegate called
subtitleView: Optional("0 rnLevel 2: 208-240V")
subtitleView: Optional("1 rnNo EV chargers")
subtitleView: Optional("2 rnLevel 1: 120 V")
<0x10703ea10> Gesture: System gesture gate timed out.
Missing MeshRenderables for ground mesh layer for (6/6) of ground tiles. Tile debug info: (Key: 0.2.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 1, PendingMaterial count: 1, Invisible MeshInstances count: 0 | Key: 0.0.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 1, PendingMaterial count: 1, Invisible MeshInstances count: 0 | Key: 1.2.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 2, PendingMaterial count: 2, Invisible MeshInstances count: 0 | Key: 1.1.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 2, PendingMaterial count: 2, Invisible MeshInstances count: 0 | Key: 1.0.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 2, PendingMaterial count: 2, Invisible MeshInstances count: 0 | Key: 0.1.2.255 t:33 kt:0, Has mesh errors: 0, MeshInstance count: 2, PendingMaterial count: 2, Invisible MeshInstances count: 0)