0

I am creating a map using swift. How can i find the name of my current location as saved on Apple maps, like lets say I am at Walmart, how do i get it to print out the name of the location, and if the location it just prints out an address.

My current code is

class MapVC: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!

let manager = CLLocationManager()


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

    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)

    let mylocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(mylocation, span)
    map.setRegion(region, animated: true)

    print(location.coordinate)
    print(location.coordinate.latitude)
    print(location.speed)

    self.map.showsUserLocation = true
    self.map.showsPointsOfInterest = true


}

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()

}
Nader Besada
  • 47
  • 1
  • 7
  • 1
    Possible duplicate of [Convert coordinates to City name?](https://stackoverflow.com/questions/27735835/convert-coordinates-to-city-name) – Emm Oct 02 '17 at 16:37

1 Answers1

-1

You can use iOS native API to convert longitude and latitude to place name:

func reverseGeocodeLocation(_ location: CLLocation, 
          completionHandler: @escaping CLGeocodeCompletionHandler)

For example:

var geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in

})

Get the place name by getting the first object of placemarks which is of type CLPlacemark. Then the CLPlacemark's name property is what you are after.

The Mach System
  • 6,703
  • 3
  • 16
  • 20