Search code examples
swiftuimapkitcore-location

Show user coordinates SwiftUI


I have custom Map with pin on user location. When i need to show user location. I tried use CLLocationManager to show coordinates but they turns 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")
            }
        }
    }
}

There is my start point

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

So i need to add user coordinates in this stroke without asking permit as i have already asked it


Solution

  • All you have to do is make the showsUserLocation var true.(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
        
    }
    

    As of iOS 14 you can use the native SwiftUI Map. This might be easier to work with than bridging to UIKit