Joe Sene Joe Sene - 2 months ago 14
Swift Question

Get users location

So I´ve been working on a task that gets the User's Location.

I worked on this code, it builds and runs nicely on the emulator.

But it does not get my current location.

Am I mssing something? If so, please help!

Here's my code:

Capture from Xcode

Answer

You can try changing your didUpdateLocation to this.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let userLocation:CLLocation = locations[0] as CLLocation

        locationManager.stopUpdatingLocation()


        let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)

        let span = MKCoordinateSpanMake(0.05, 0.05)

        let region = MKCoordinateRegion(center: location, span: span)

        myMap.setRegion(region, animated: true)


    }

Hope it will help you.

Comments