UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Map (not using UIRepresentative) - Get it to move to your current location on the map.

Forums > SwiftUI

I have the info.plist set for when using the app. That doesn't seem to be a problem. But for some reason I'm missing something and I can't get the current location to show on the map. It shows a blue dot where my current location is, but it doesn't move to the blue dot. I've tried with with and without the CLLocation, and I get the same result. Blue dot at my currently location, but it doesn't move to it.

struct Home:View {

    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 13.086, longitude: 80.2769), latitudinalMeters: 10000, longitudinalMeters: 10000)
    @State var trackingMode: MapUserTrackingMode = .follow
    @State var manager = CLLocationManager()
    @StateObject var managerDelegate = LocationDelegate()

    var body:some View {
        VStack {
            Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: $trackingMode)
        }
        .onAppear {
            manager.delegate = managerDelegate
        }
    }
}
class LocationDelegate: NSObject, ObservableObject, CLLocationManagerDelegate {

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if manager.authorizationStatus == .authorizedWhenInUse {
            print("Authorized...")
            manager.startUpdatingLocation()
        } else {
            print("Not Authorized...")
            manager.requestWhenInUseAuthorization()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Updated Location")
    }
}

3      

This appears to be the answer. Moved the region to the LocationDelegate and used the didUpdateLocation to update the lat / long for the region. If someone has a better way to do it, let me know. I'll mark it as solved in a week for others if not.

struct Home:View {

    @State var trackingMode: MapUserTrackingMode = .follow
    @State var manager = CLLocationManager()
    @StateObject var managerDelegate = LocationDelegate()

    var body:some View {
        VStack {
            Map(coordinateRegion: $managerDelegate.region, interactionModes: .all, showsUserLocation: true, userTrackingMode: $trackingMode)
        }
        .onAppear {
            manager.delegate = managerDelegate
        }
    }
}

class LocationDelegate: NSObject, ObservableObject, CLLocationManagerDelegate {

    @Published var  region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 13.086, longitude: 80.2769), latitudinalMeters: 10000, longitudinalMeters: 10000)

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        if manager.authorizationStatus == .authorizedWhenInUse {
            print("Authorized...")
            manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            manager.startUpdatingLocation()
        } else {
            print("Not Authorized...")
            manager.requestWhenInUseAuthorization()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Updated Location")
        region.center.latitude = (manager.location?.coordinate.latitude)!
        region.center.longitude = (manager.location?.coordinate.longitude)!
    }
}

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.