Mapkit 如果不桥接到ObjectiveC,我们能否仅在SwiftUI中获取水龙头的坐标?
我有以下代码:Mapkit 如果不桥接到ObjectiveC,我们能否仅在SwiftUI中获取水龙头的坐标?,mapkit,swiftui,mapkitannotation,Mapkit,Swiftui,Mapkitannotation,我有以下代码: struct MyLocationMap: View { @EnvironmentObject var userData: UserData @State var annotationArray: [MyAnnotation] = [MyAnnotation(coordinates: CLLocationCoordinate2D(latitude: CurrentLocation().coordinates?.latitude ?? CLLocation
struct MyLocationMap: View {
@EnvironmentObject var userData: UserData
@State var annotationArray: [MyAnnotation] = [MyAnnotation(coordinates: CLLocationCoordinate2D(latitude: CurrentLocation().coordinates?.latitude ?? CLLocationCoordinate2D().latitude, longitude: CurrentLocation().coordinates?.longitude ?? CLLocationCoordinate2D().longitude), type: .waypoint)]
var body: some View {
MakeMapView(annotationArray: annotationArray)
.gesture(
LongPressGesture(minimumDuration: 1)
.onEnded { _ in
print("MapView pressed!\n--------------------------------------\n")
//Handle press here
})
}
}
import SwiftUI
import CoreLocation
import MapKit
struct MakeMapView : UIViewRepresentable {
typealias UIViewType = MKMapView
let annotationArray: [MyAnnotation]?
func makeUIView(context: UIViewRepresentableContext<MakeMapView>) -> MKMapView{
MKMapView()
}
func updateUIView(_ mapView: MKMapView, context: Context) {
mapView.showsUserLocation = true
if let coordinates = CurrentLocation().coordinates {
// updateAnnotations(from: mapView)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
mapView.showsUserLocation = true
mapView.showsCompass = true
mapView.showsScale = true
mapView.mapType = .satellite
let span = MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)
let region = MKCoordinateRegion(center: coordinates, span: span)
mapView.setRegion(region, animated: true)
})
}
}
struct MyLocationMap:视图{
@环境对象变量userData:userData
@状态变量annotationArray:[MyAnnotation]=[MyAnnotation(坐标:CLLocationCoordinate2D(纬度:CurrentLocation().坐标?.纬度??CLLocationCoordinate2D().纬度,经度:CurrentLocation().坐标?.经度??CLLocationCoordinate2D().经度),类型:.航路点]
var body:一些观点{
MakeMapView(annotationArray:annotationArray)
.手势(
长按手势(最短持续时间:1)
.onEnded{uuin
打印(“已按地图视图!\n---------------------------------------------\n”)
//在这里按把手
})
}
}
导入快捷键
导入核心定位
导入地图套件
结构MakeMapView:UIViewRepresentable{
typealias UIViewType=MKMapView
是否让annotationArray:[MyAnnotation]?
func makeUIView(上下文:UIViewRepresentableContext)->MKMapView{
MKMapView()
}
func updateUIView(uMapView:MKMapView,context:context){
mapView.showsUserLocation=true
如果let坐标=CurrentLocation().坐标{
//更新注释(来自:mapView)
DispatchQueue.main.asyncAfter(截止日期:.now()+1.0,执行:{
mapView.showsUserLocation=true
mapView.showsCompass=true
mapView.showscale=true
mapView.mapType=.satellite
设span=MKCoordinateSpan(相对偏差:0.002,纵向偏差:0.002)
let region=MKCoordinateRegion(中心:坐标,跨度:跨度)
mapView.setRegion(区域,动画:true)
})
}
}
我遇到的问题是实现func convert(upoint:CGPoint,toCoordinateFrom view:UIView?->CLLocationCoordinate2D
仅使用SwiftUI/Combine获取手势的纬度/长度。是否可能?如果不可能,我可以用我现有的实现它吗
我已经在
及
一旦我有了坐标,放下大头针是很简单的,但我不能绕着头去获取坐标
谢谢。没有最小距离的Dragesture会立即激活,并且在其更改后的侦听器中有位置和惊人位置,您可以使用它来获取位置,如下所示:
struct GesturesView: View {
@State private var location: CGPoint = .zero
var body: some View {
let drag = DragGesture(minimumDistance: 0).onChanged {
self.location = $0.startLocation
}
let longPress = LongPressGesture().onEnded { _ in
self.doSomething(with: self.location)
}
let gesture = longPress.simultaneously(with: drag)
return Rectangle()
.frame(width: 300, height: 300)
.padding()
.gesture(gesture)
}
func doSomething(with location: CGPoint) {
print("Pressed at", location)
}
}