0

Is there an alternative way to get the user's latitude and longitude without using the function didUpdateLocations. I only want to do this once and set the initial region when the view is loaded, not every time the user's location is updated.

I know I could get the user's Lat and Lng with the code below but is there an alternative?

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

    var userLocation:CLLocation = locations[0] as CLLocation

    var latitude:CLLocationDegrees = userLocation.coordinate.latitude

    var longitude:CLLocationDegrees = userLocation.coordinate.longitude

    var latDelta:CLLocationDegrees = 0.01

    var lonDelta:CLLocationDegrees = 0.01

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    myMap.setRegion(region, animated: true)

}
Pablo Picasso
  • 133
  • 2
  • 12

1 Answers1

0

There is for iOS 9: CLLocationManager.requestLocation(). This API is provided just for your purpose - a way to grab a location at a discreet time without turning on continuous tracking.

See this page for a preview on how it works.

In iOS 8 and lower, I am not aware of an API.

justinpawela
  • 1,968
  • 1
  • 14
  • 18