【问题标题】:Show user coordinates SwiftUI显示用户坐标 SwiftUI
【发布时间】:2021-08-30 14:31:52
【问题描述】:

我有自定义地图,用户位置上有别针。当我需要显示用户位置时。我尝试使用 CLLocationManager 显示坐标,但它们变成 0.0

struct MapView : UIViewRepresentable {
    
    func makeCoordinator() -> MapView.Coordinator {
        return MapView.Coordinator(parent1: self)
    }
    
    @Binding var title : String
    @Binding var subtitle : String
    
    func makeUIView(context: UIViewRepresentableContext<MapView>) ->  MKMapView {
        
        let map = MKMapView()
        let coordinate = CLLocationCoordinate2D(latitude: 55.757485,
                                               longitude: 37.632179)
        map.region = MKCoordinateRegion(center: coordinate,
                                        latitudinalMeters: 100,
                                        longitudinalMeters: 100)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        
        map.delegate = context.coordinator
            
        map.addAnnotation(annotation)
        return map
    }
    
    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {}

    class Coordinator: NSObject , MKMapViewDelegate {
        
        var parent : MapView
        init(parent1: MapView) {
            parent = parent1
        }
        
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            
            let pin = MKPinAnnotationView(annotation: annotation,
                                          reuseIdentifier: "pin")
            pin.isDraggable = true
            pin.pinTintColor = .blue
            pin.animatesDrop = true
            
            return pin
        }
        
        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationView.DragState, fromOldState oldState: MKAnnotationView.DragState) {
            
            CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: (view.annotation?.coordinate.latitude)!,
                                               longitude: (view.annotation?.coordinate.longitude)!)){ (places, err) in
                
                if err != nil {
                    print((err?.localizedDescription)!)
                    return
                }
                
                self.parent.title = (places?.first?.name ?? places?.first?.postalCode)!
                self.parent.subtitle = (places?.first?.locality ?? places?.first?.country ?? "None")
            }
        }
    }
}

这是我的起点

let coordinate = CLLocationCoordinate2D(latitude: 55.757485,
                                        longitude: 37.632179)

所以我需要在这个笔划中添加用户坐标而不需要我已经询问过的许可

【问题讨论】:

    标签: swiftui mapkit core-location


    【解决方案1】:

    您所要做的就是使showsUserLocation var 为真。(Look at the docs)

    func makeUIView(context: UIViewRepresentableContext<MapView>) ->  MKMapView {
        
        let map = MKMapView()
        let coordinate = CLLocationCoordinate2D(latitude: 55.757485, longitude: 37.632179)
        map.region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 100, longitudinalMeters: 100)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
    
        // new code...
        map.showsUserLocation = true
    
        map.delegate = context.coordinator
            
        map.addAnnotation(annotation)
        return map
        
    }
    

    从 iOS 14 开始,您可以使用原生 SwiftUI Map。这可能比桥接到 UIKit 更容易使用

    【讨论】:

    • 是的,我可以做到,但 pin 不在用户位置上
    • 你在用模拟器吗?
    • 我测试了我的手机,它显示了我的位置,但我需要将我的密码与我的坐标连接起来。这就是现在的问题
    • 您上面的代码可以很好地添加一个 pin,您确定您正在获取用户的位置吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多