首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在mapView上点击注释以获得详细视图时,快速设置条件语句

在mapView上点击注释以获得详细视图时,快速设置条件语句
EN

Stack Overflow用户
提问于 2022-08-24 14:35:17
回答 1查看 73关注 0票数 0

我有一个带有几个自定义注释的mapView。现在我想要的是当我点击一个注释时,条件语句voor showLocation: String被设置为一个特定的值。

例如,annotation.title =未来的公司,showLocation变量被设置为“未来的公司”。当我点击标题为"Tip 1“的注释时,showLocation设置为"Tip 1”。

当您点击一个注释时,就会显示一个工作表。这是可行的,但我想在每个注释中显示一些不同的信息。

所以我介绍了MapTipView。

我试过什么?很多事情..。我也找到了一些信息,但其中大部分是为故事板。

我尝试将showLocation = "value“放入下面的函数中。但这只起作用一次。当您单击另一个注释时,仍然会得到您单击的第一个注释的值。

代码语言:javascript
运行
复制
 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
                let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TESTING NOTE")
                annotationView.canShowCallout = true
        
        
    if annotation.title == "Start" {
                annotationView.image = UIImage(named: "PictoStart")
            let size = CGSize(width: 80, height: 80)
            annotationView.image = UIGraphicsImageRenderer(size:size).image {
                _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
               
}
       showLocation = "Start"
        
    } else if annotation.title == "Firm of the Future" {
            annotationView.image = UIImage(systemName: "house.fill")?.withTintColor(UIColor(red: 0.13, green: 0.505, blue: 0.261, alpha: 10), renderingMode: .alwaysOriginal)
            let size = CGSize(width: 50, height: 50)
            annotationView.image = UIGraphicsImageRenderer(size:size).image {
                _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
                
            }
          showLocation = "Firm of the Future" 
       }
        else if annotation.title == "Tip 1" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        showLocation = "Tip 1"
}

// and so for all annotations

我试过了,但也没用:

代码语言:javascript
运行
复制
struct MapTipView: View {
    @Binding var showActionSheet: Bool
    @Binding var showLocation: String
    
    @State var annotation = MKPointAnnotation()

var body: some View {
        ZStack {
            Image("Achtergrond")
                .resizable()
                .ignoresSafeArea()
            
            VStack {
                
                if annotation.title == "Start" {
                    Text("Start")
                } else {
                    Text("Fout")
                }
            }
        }
    }
}

这是我的mapView的完整代码:

代码语言:javascript
运行
复制
import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {
  
    let view = UIView()
  let region: MKCoordinateRegion
  let lineCoordinates: [CLLocationCoordinate2D]
    @Binding var manager : CLLocationManager
    @Binding var alert : Bool
    @Binding var currentLocation : CLLocationCoordinate2D
    
    @State private var UserTrackingMode: MKUserTrackingMode = .follow
    @Binding var showActionSheet: Bool
    @Binding var showLocation: String
    @Binding var showingPlaceDetails: MKPointAnnotation?

  func makeUIView(context: Context) -> MKMapView {
      let mapView = context.environment.mkMapView
    mapView.delegate = context.coordinator
    mapView.region = region
      mapView.showsScale = true
              
      mapView.showsCompass = true
      mapView.showsUserLocation = true
      manager.requestWhenInUseAuthorization()
      manager.delegate = context.coordinator as? CLLocationManagerDelegate
      manager.startUpdatingLocation()

      
      let FOTF = MKPointAnnotation()
      FOTF.title = "Firm of the Future"
      FOTF.coordinate = CLLocationCoordinate2D(latitude: 51.98837, longitude: 5.81737)
      mapView.addAnnotation(FOTF)
      
      let Start = MKPointAnnotation()
      Start.title = "Start"
      Start.coordinate = CLLocationCoordinate2D(latitude: 51.98847388992212, longitude: 5.817359983921051)
      mapView.addAnnotation(Start)
      
      let Tip1 = MKPointAnnotation()
      Tip1.title = "Tip 1"
      Tip1.subtitle = "Hier krijg je meer informatie over de schuilplaats."
      Tip1.coordinate = CLLocationCoordinate2D(latitude: 51.989511, longitude: 5.818118)
      mapView.addAnnotation(Tip1)
      
      let Tip2 = MKPointAnnotation()
      Tip2.title = "Tip 2"
      Tip2.subtitle = "Hier krijg je meer informatie over het wapen."
      Tip2.coordinate = CLLocationCoordinate2D(latitude: 51.993546, longitude: 5.818026)
      mapView.addAnnotation(Tip2)
      
      let Tip3 = MKPointAnnotation()
      Tip3.title = "Tip 3"
      Tip3.subtitle = "Hier krijg je meer informatie over de handlanger."
      Tip3.coordinate = CLLocationCoordinate2D(latitude: 51.994310, longitude: 5.816085)
      mapView.addAnnotation(Tip3)
      
      let Brief1 = MKPointAnnotation()
      Brief1.title = "Brief 1"
      Brief1.subtitle = "Hier krijg je een brief, los het raadsel op."
      Brief1.coordinate = CLLocationCoordinate2D(latitude: 51.996232, longitude:  5.818456)
      mapView.addAnnotation(Brief1)
      
      let Tip4 = MKPointAnnotation()
      Tip4.title = "Tip 4"
      Tip4.subtitle = "Hier krijg je meer informatie over de mogelijke dader."
      Tip4.coordinate = CLLocationCoordinate2D(latitude: 51.997561, longitude: 5.818167)
      mapView.addAnnotation(Tip4)
      
      let Tip5 = MKPointAnnotation()
      Tip5.title = "Tip 5"
      Tip5.subtitle = "Hier krijg je meer informatie over het wapen."
      Tip5.coordinate = CLLocationCoordinate2D(latitude: 51.998065, longitude: 5.820206)
      mapView.addAnnotation(Tip5)
      
      let Tip6 = MKPointAnnotation()
      Tip6.title = "Tip 6"
      Tip6.subtitle = "Hier krijg je meer informatie over de schuilplaats."
      Tip6.coordinate = CLLocationCoordinate2D(latitude: 51.995915, longitude: 5.828055)
      mapView.addAnnotation(Tip6)
      
      let Tip7 = MKPointAnnotation()
      Tip7.title = "Tip 7"
      Tip7.subtitle = "Hier krijg je meer informatie over de handlanger."
      Tip7.coordinate = CLLocationCoordinate2D(latitude: 51.995339, longitude: 5.831488)
      mapView.addAnnotation(Tip7)
      
      let Tip8 = MKPointAnnotation()
      Tip8.title = "Tip 8"
      Tip8.subtitle = "Hier krijg je meer informatie over de schuilplaats."
      Tip8.coordinate = CLLocationCoordinate2D(latitude: 51.995415, longitude: 5.837951)
      mapView.addAnnotation(Tip8)
      
      let Tip9 = MKPointAnnotation()
      Tip9.title = "Tip 9"
      Tip9.subtitle = "Hier krijg je meer informatie over de handlanger."
      Tip9.coordinate = CLLocationCoordinate2D(latitude: 51.994350, longitude: 5.837638)
      mapView.addAnnotation(Tip9)
      
      let Tip10 = MKPointAnnotation()
      Tip10.title = "Tip 10"
      Tip10.subtitle = "Hier krijg je meer informatie over de mogelijke dader."
      Tip10.coordinate = CLLocationCoordinate2D(latitude: 51.992861, longitude: 5.838441)
      mapView.addAnnotation(Tip10)
      
      let Brief2 = MKPointAnnotation()
      Brief2.title = "Brief 2"
      Brief2.subtitle = "Hier krijg je een brief, los het raadsel op."
      Brief2.coordinate = CLLocationCoordinate2D(latitude: 51.991260, longitude: 5.837517)
      mapView.addAnnotation(Brief2)
      
      let Tip11 = MKPointAnnotation()
      Tip11.title = "Tip 11"
      Tip11.subtitle = "Hier krijg je meer informatie over het wapen."
      Tip11.coordinate = CLLocationCoordinate2D(latitude: 51.988222, longitude: 5.835309)
      mapView.addAnnotation(Tip11)
      
      let Tip12 = MKPointAnnotation()
      Tip12.title = "Tip 12"
      Tip12.subtitle = "Hier krijg je meer informatie over de handlanger."
      Tip12.coordinate = CLLocationCoordinate2D(latitude: 51.987629, longitude: 5.832252)
      mapView.addAnnotation(Tip12)
      
      let Tip13 = MKPointAnnotation()
      Tip13.title = "Tip 13"
      Tip13.subtitle = "Hier krijg je meer informatie over de schuilplaats."
      Tip13.coordinate = CLLocationCoordinate2D(latitude: 51.982672, longitude: 5.830202)
      mapView.addAnnotation(Tip13)
      
      let Tip14 = MKPointAnnotation()
      Tip14.title = "Tip 14"
      Tip14.subtitle = "Hier krijg je meer informatie over de mogelijke dader."
      Tip14.coordinate = CLLocationCoordinate2D(latitude: 51.983640, longitude:  5.826948)
      mapView.addAnnotation(Tip14)
      
      let Tip15 = MKPointAnnotation()
      Tip15.title = "Tip 15"
      Tip15.subtitle = "Hier krijg je meer informatie over het wapen."
      Tip15.coordinate = CLLocationCoordinate2D(latitude: 51.982390, longitude:  5.825463)
      mapView.addAnnotation(Tip15)
      
      let Tip16 = MKPointAnnotation()
      Tip16.title = "Tip 16"
      Tip16.subtitle = "Hier krijg je meer informatie over de mogelijke dader."
      Tip16.coordinate = CLLocationCoordinate2D(latitude: 51.978387, longitude:  5.826538)
      mapView.addAnnotation(Tip16)
      
      let Brief3 = MKPointAnnotation()
      Brief3.title = "Brief 3"
      Brief3.subtitle = "Hier krijg je een brief, los het raadsel op."
      Brief3.coordinate = CLLocationCoordinate2D(latitude: 51.975684, longitude:  5.826141)
      mapView.addAnnotation(Brief3)
      
      let Tip17 = MKPointAnnotation()
      Tip17.title = "Tip 17"
      Tip17.subtitle = "Hier krijg je meer informatie over de handlanger."
      Tip17.coordinate = CLLocationCoordinate2D(latitude: 51.974398, longitude:  5.820770)
      mapView.addAnnotation(Tip17)
      
      let Tip18 = MKPointAnnotation()
      Tip18.title = "Tip 18"
      Tip18.subtitle = "Hier krijg je meer informatie over de mogelijke dader."
      Tip18.coordinate = CLLocationCoordinate2D(latitude: 51.978024, longitude:  5.819883)
      mapView.addAnnotation(Tip18)
      
      let Tip19 = MKPointAnnotation()
      Tip19.title = "Tip 19"
      Tip19.subtitle = "Hier krijg je meer informatie over het wapen."
      Tip19.coordinate = CLLocationCoordinate2D(latitude: 51.980607, longitude:  5.818235)
      mapView.addAnnotation(Tip19)
      
      let Tip20 = MKPointAnnotation()
      Tip20.title = "Tip 20"
      Tip20.subtitle = "Hier krijg je meer informatie over de schuilplaats."
      Tip20.coordinate = CLLocationCoordinate2D(latitude: 51.984846, longitude: 5.818178)
      mapView.addAnnotation(Tip20)
      
      let Brief4 = MKPointAnnotation()
      Brief4.title = "Brief 4"
      Brief4.subtitle = "Hier krijg je een brief, los het raadsel op."
      Brief4.coordinate = CLLocationCoordinate2D(latitude: 51.987069, longitude: 5.817966)
      mapView.addAnnotation(Brief4)
      
      let Finish = MKPointAnnotation()
      Finish.title = "Finish"
      Finish.coordinate = CLLocationCoordinate2D(latitude: 51.98831387558797, longitude: 5.817681513726711)
      mapView.addAnnotation(Finish)
      
      

   
    let polyline = MKPolyline(coordinates: lineCoordinates, count: lineCoordinates.count)
    mapView.addOverlay(polyline)

    return mapView
      
  }

    
  func updateUIView(_ view: MKMapView, context: Context) {}
    
  func makeCoordinator() -> Coordinator {
      
      Coordinator(self, showActionSheet: $showActionSheet, showLocation: $showLocation )
  }
    
    
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation, context: Context, didUpdate userLocation: MKUserLocation) -> MKAnnotationView? {
        
        mapView.showsUserLocation = true
        guard annotation is MKPointAnnotation else { return nil }

        let identifier = "Annotation"
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        } else {
            annotationView!.annotation = annotation
        }
       // guard !annotation.isKind(of: MKUserLocation.self) else { return nil }
        
        return annotationView
    }

}

class Coordinator: NSObject, MKMapViewDelegate {
    @Binding var showActionSheet: Bool
    @Binding var showLocation: String
   
   // @Binding var StartAnnotation: Bool
    
    let uiView = UIView()
    var parent: MapView

    init(_ parent: MapView, showActionSheet: Binding<Bool>, showLocation: Binding<String>) {
    self.parent = parent
      _showActionSheet = showActionSheet
        _showLocation = showLocation
  }

  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let routePolyline = overlay as? MKPolyline {
      let renderer = MKPolylineRenderer(polyline: routePolyline)
        renderer.strokeColor = UIColor.red
      renderer.lineWidth = 7
      return renderer
    }
    return MKOverlayRenderer()
  }
    
    
    
    func mapView(_ mapView: MKMapView,
                         didUpdate userLocation: MKUserLocation) {
        //mapView.userTrackingMode = .follow
       // mapView.showsUserLocation = true
                print("User location\(userLocation.coordinate)")
            }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        
        if status == .denied{
            
            parent.alert.toggle()
        }
    }
    
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
                let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TESTING NOTE")
                annotationView.canShowCallout = true
        
        
    if annotation.title == "Start" {
                annotationView.image = UIImage(named: "PictoStart")
            let size = CGSize(width: 80, height: 80)
            annotationView.image = UIGraphicsImageRenderer(size:size).image {
                _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
               
}
        
    } else if annotation.title == "Firm of the Future" {
            annotationView.image = UIImage(systemName: "house.fill")?.withTintColor(UIColor(red: 0.13, green: 0.505, blue: 0.261, alpha: 10), renderingMode: .alwaysOriginal)
            let size = CGSize(width: 50, height: 50)
            annotationView.image = UIGraphicsImageRenderer(size:size).image {
                _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
                
            }
       }
        else if annotation.title == "Tip 1" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 2" {
                    annotationView.image = UIImage(named: "PictoWapen")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 3" {
                    annotationView.image = UIImage(named: "PictoHandlanger")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Brief 1" {
                    annotationView.image = UIImage(named: "PictoBrief1")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 4" {
                    annotationView.image = UIImage(named: "PictoDader")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 5" {
                    annotationView.image = UIImage(named: "PictoWapen")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 6" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 7" {
                    annotationView.image = UIImage(named: "PictoHandlanger")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 8" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 9" {
                    annotationView.image = UIImage(named: "PictoHandlanger")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 10" {
                    annotationView.image = UIImage(named: "PictoDader")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Brief 2" {
                    annotationView.image = UIImage(named: "PictoBrief2")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 11" {
                    annotationView.image = UIImage(named: "PictoWapen")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 12" {
                    annotationView.image = UIImage(named: "PictoHandlanger")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 13" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 14" {
                    annotationView.image = UIImage(named: "PictoDader")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 15" {
                    annotationView.image = UIImage(named: "PictoWapen")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 16" {
                    annotationView.image = UIImage(named: "PictoDader")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Brief 3" {
                    annotationView.image = UIImage(named: "PictoBrief3")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 17" {
                    annotationView.image = UIImage(named: "PictoHandlanger")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 18" {
                    annotationView.image = UIImage(named: "PictoDader")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 19" {
                    annotationView.image = UIImage(named: "PictoWapen")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Tip 20" {
                    annotationView.image = UIImage(named: "PictoSchuilplaats")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
    }
        } else if annotation.title == "Brief 4" {
                    annotationView.image = UIImage(named: "PictoBrief4")
                let size = CGSize(width: 80, height: 80)
                annotationView.image = UIGraphicsImageRenderer(size:size).image {
                    _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
                }
                    
    } else if annotation.title == "Finish" {
        annotationView.image = UIImage(named: "PictoFinish")
    let size = CGSize(width: 80, height: 80)
    annotationView.image = UIGraphicsImageRenderer(size:size).image {
        _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
}
            
        } else {
            
            annotationView.image = UIImage(named: "SilMap")
        let size = CGSize(width: 80, height: 80)
        annotationView.image = UIGraphicsImageRenderer(size:size).image {
            _ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
        }
            
        
        }
            
                
                return annotationView
    }
    
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
       // showActionSheet.toggle()
        print("calloutAccessoryControlTapped")
        
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        
        showActionSheet.toggle()
    
        print("didSelectAnnotationTapped")
    }
}



struct EnvironmentMKMapView: EnvironmentKey {
    typealias Value = MKMapView

    static var defaultValue = MKMapView()
}

extension EnvironmentValues {
    var mkMapView: MKMapView {
        get { self[EnvironmentMKMapView.self] }
        set { self[EnvironmentMKMapView.self] = newValue }
    }
}


struct UserTrackingButton: UIViewRepresentable {

    typealias UIViewType = MKUserTrackingButton

    func makeUIView(context: Context) -> MKUserTrackingButton {
        MKUserTrackingButton(mapView: context.environment.mkMapView)
    }

    func updateUIView(_ view: MKUserTrackingButton, context: Context) { }
}

这是我的MapViewContent的代码:

代码语言:javascript
运行
复制
import SwiftUI
import MapKit

struct MapViewContent: View {
 
    @State var region = MKCoordinateRegion(
      // FOTF
      center: CLLocationCoordinate2D(latitude: 51.98495, longitude: 5.82808),
      span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)
    )
    
    @State var manager = CLLocationManager()
    @State var alert = false
    @State var currentLocation = CLLocationCoordinate2D()
    
    @State private var UserTrackingMode: MKUserTrackingMode = .none
    @State var showActionSheet: Bool = false
    @State var showLocation: String = ""
    @State var showingPlaceDetails: MKPointAnnotation?
    
  

  @State private var lineCoordinates = [

    // Firm of the Future
        CLLocationCoordinate2D(latitude: 51.98847388992212, longitude: 5.817359983921051),
       //and more coordinates for a Polyline
  ];

  var body: some View {
      
      ZStack(alignment: .bottom) {
        
              MapView(
        region: region,
        lineCoordinates: lineCoordinates, manager: $manager, alert: $alert, currentLocation: $currentLocation, showActionSheet: $showActionSheet, showLocation: $showLocation, showingPlaceDetails: $showingPlaceDetails
    )
      .edgesIgnoringSafeArea(.all)
              
              HStack {
                  
                  Spacer()
                  Text("Locatie volgen:")
                      .font(.headline)
                      //.padding(.horizontal)
                  
                  UserTrackingButton()
                      .frame(width: 50, height: 50)
                     // .padding(.horizontal)

                                          
              }.padding()
      }
      .sheet(isPresented: $showActionSheet) {
       MapTipView(showActionSheet: $showActionSheet, showLocation: $showLocation)
      }
      }
      
  }

这是我的MapTipView的代码(随表显示的代码):

代码语言:javascript
运行
复制
import SwiftUI
import MapKit

struct MapTipView: View {
    @Binding var showActionSheet: Bool
    @Binding var showLocation: String
    
    var body: some View {
        ZStack {
            Image("Achtergrond")
                .resizable()
                .ignoresSafeArea()
            
            VStack {
                
                
                if showLocation == "Start" {
                    Text("Start")
                }
                
                if showLocation == "Firm of the Future" {
                    Text("Firm of the Future")
                }
                
                if showLocation == "Tip 1" {
                    Text("Tip 1")
                }

// and so on
                
                else {
                    Text("Fout")
                }
            }
        }
    }
}

有谁能帮我了解每一个注释的详细视图吗?谢谢是先进的。

编辑:这就是我对didSelect所做的:

代码语言:javascript
运行
复制
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        let annotation = MKPointAnnotation()
        showActionSheet.toggle()
        
        if annotation.title == "Start" {
            showLocation = "Start"
        }
        if annotation.title == "Firm of the Future" {
            showLocation = "Firm of the Future"
        }
        if annotation.title == "Tip 1" {
            showLocation = "Tip 1"
        }
        //and so on for all annotations
        
    
        print("didSelectAnnotationTapped")
    }
EN

回答 1

Stack Overflow用户

发布于 2022-08-24 15:23:30

不要试图在view(for:)方法中记录当前的注释。

相反,将视图控制器(或其他一些对象)设置为映射视图的委托,并实现来自MKMapViewDelegate协议的MKMapViewDelegate。当该方法被调用时,记录被点击的注释。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73475054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档