gpt4 book ai didi

mapkit - 您如何让用户向 map 添加图钉并在 SwiftUI 中获取坐标?

转载 作者:行者123 更新时间:2023-12-03 14:38:34 33 4
gpt4 key购买 nike

我在以芝加哥为中心的 View 中有一张 map 。我希望用户能够在 map 上放置图钉/注释,然后检索这些坐标。 map 加载芝加哥很好,但我无法使注释代码起作用。

我似乎找不到 SwiftUI 的答案,特别是。只有 Swift 和 Storyboards。我觉得我有 99% 的代码,但这些片段不在正确的位置。我包含了错误所在位置的屏幕截图。谢谢你的帮助。

import SwiftUI
import MapKit

struct EntireMapView: UIViewRepresentable {

func updateUIView(_ mapView: MKMapView, context: Context) {

mapView.delegate = context.coordinator

let longPressGesture = UILongPressGestureRecognizer(target: mapView, action: #selector(EntireMapViewCoordinator.addAnnotation(gesture:)))
mapView.addGestureRecognizer(longPressGesture)

let span = MKCoordinateSpan(latitudeDelta: 0.3, longitudeDelta: 0.3)

var chicagoCoordinate = CLLocationCoordinate2D()
chicagoCoordinate.latitude = 41.878113
chicagoCoordinate.longitude = -87.629799

let region = MKCoordinateRegion(center: chicagoCoordinate, span: span)

mapView.setRegion(region, animated: true)

}

func makeUIView(context: Context) -> MKMapView {

let mapView = MKMapView(frame: .zero)
mapView.delegate = context.coordinator
return mapView

}

func makeCoordinator() -> EntireMapViewCoordinator {
return EntireMapViewCoordinator(self)
}

class EntireMapViewCoordinator: NSObject, MKMapViewDelegate {

var entireMapViewController: EntireMapView

init(_ control: EntireMapView) {
self.entireMapViewController = control
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return (annotation as? MKAnnotationView)!
}

@objc func addAnnotation(gesture: UILongPressGestureRecognizer) {

if gesture.state == .ended {

let point = gesture.location(in: self.mapView) **<--- ERROR HERE**
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)

var annotation = MKPointAnnotation()
annotation.coordinate = coordinate

self.mapView.addAnnotation(annotation) **<--- ERROR HERE**
}
}
}
}

struct EntireMapView_Previews: PreviewProvider {
static var previews: some View {
EntireMapView()
}
}

Here's a screen shot of the errors

最佳答案

这是答案的完整代码。功劳主要归功于 @yeezy ,因为通过此行获取 View :let mapView = gestureRecognizer.view as? MKMApView是问题中的主要错误。

  struct EntireMapView: UIViewRepresentable {

func updateUIView(_ mapView: MKMapView, context: Context) {

let span = MKCoordinateSpan(latitudeDelta: 0.3, longitudeDelta: 0.3)
var chicagoCoordinate = CLLocationCoordinate2D()
chicagoCoordinate.latitude = 41.878113
chicagoCoordinate.longitude = -87.629799
let region = MKCoordinateRegion(center: chicagoCoordinate, span: span)
mapView.setRegion(region, animated: true)

}

func makeUIView(context: Context) -> MKMapView {

let myMap = MKMapView(frame: .zero)
let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(EntireMapViewCoordinator.addAnnotation(gesture:)))
longPress.minimumPressDuration = 1
myMap.addGestureRecognizer(longPress)
myMap.delegate = context.coordinator
return myMap

}

func makeCoordinator() -> EntireMapViewCoordinator {
return EntireMapViewCoordinator(self)
}

class EntireMapViewCoordinator: NSObject, MKMapViewDelegate {

var entireMapViewController: EntireMapView

init(_ control: EntireMapView) {
self.entireMapViewController = control
}


@objc func addAnnotation(gesture: UIGestureRecognizer) {

if gesture.state == .ended {

if let mapView = gesture.view as? MKMapView {
let point = gesture.location(in: mapView)
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
}
}
}
}

关于mapkit - 您如何让用户向 map 添加图钉并在 SwiftUI 中获取坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59182557/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com