获取地理信息(swift)

前端之家收集整理的这篇文章主要介绍了获取地理信息(swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

获取地理信息(swift)

  • 1.insert
CLLocationManagerDelegate
  • 2.初始化
var locationManager: CLLocationManager!

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
  • 3.delegate
func locationManager(manager: CLLocationManager,didFailWithError error: NSError) {

}

func locationManager(manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    CLGeocoder().reverseGeocodeLocation(manager.location!,completionHandler: {(placemarks,error)->Void in if (error != nil) { return } if placemarks!.count > 0 { let pm = placemarks![0] self.displayLocationInfo(pm) } }) } func displayLocationInfo(placemark: CLPlacemark?) { if let containsPlacemark = placemark { locationManager.stopUpdatingLocation() let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : "" let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : "" let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : "" let country = (containsPlacemark.country != nil) ? containsPlacemark.country : "" } }

猜你在找的Swift相关文章