WWDC21 SALE: Save 50% on all my Swift books and bundles! >>

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")
    }
}

   

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)!
    }
}

   

Save money with our WWDC sale!

SAVE 50% To celebrate WWDC21, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.