Swift - 经纬度位置坐标与真实地理位置相互转化
(本文代码已升级至Swift3)
通过 CoreLocation 类,得到的定位信息都是以经度和纬度等表示的地理信息,通过 CLGeocoder 类可以将其反编码成一个地址。反之,也可根据一个地址获取经纬度。
通过 CoreLocation 类,得到的定位信息都是以经度和纬度等表示的地理信息,通过 CLGeocoder 类可以将其反编码成一个地址。反之,也可根据一个地址获取经纬度。
1,通过经纬度获取地址
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController ,CLLocationManagerDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
reverseGeocode()
}
//地理信息反编码
func reverseGeocode(){
let geocoder = CLGeocoder()
let currentLocation = CLLocation(latitude: 32.029171, longitude: 118.788231)
geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
(placemarks:[CLPlacemark]?, error:Error?) -> Void in
//强制转成简体中文
let array = NSArray(object: "zh-hans")
UserDefaults.standard.set(array, forKey: "AppleLanguages")
//显示所有信息
if error != nil {
//print("错误:\(error.localizedDescription))")
self.textView.text = "错误:\(error!.localizedDescription))"
return
}
if let p = placemarks?[0]{
print(p) //输出反编码信息
var address = ""
if let country = p.country {
address.append("国家:\(country)\n")
}
if let administrativeArea = p.administrativeArea {
address.append("省份:\(administrativeArea)\n")
}
if let subAdministrativeArea = p.subAdministrativeArea {
address.append("其他行政区域信息(自治区等):\(subAdministrativeArea)\n")
}
if let locality = p.locality {
address.append("城市:\(locality)\n")
}
if let subLocality = p.subLocality {
address.append("区划:\(subLocality)\n")
}
if let thoroughfare = p.thoroughfare {
address.append("街道:\(thoroughfare)\n")
}
if let subThoroughfare = p.subThoroughfare {
address.append("门牌:\(subThoroughfare)\n")
}
if let name = p.name {
address.append("地名:\(name)\n")
}
if let isoCountryCode = p.isoCountryCode {
address.append("国家编码:\(isoCountryCode)\n")
}
if let postalCode = p.postalCode {
address.append("邮编:\(postalCode)\n")
}
if let areasOfInterest = p.areasOfInterest {
address.append("关联的或利益相关的地标:\(areasOfInterest)\n")
}
if let ocean = p.ocean {
address.append("海洋:\(ocean)\n")
}
if let inlandWater = p.inlandWater {
address.append("水源,湖泊:\(inlandWater)\n")
}
self.textView.text = address
} else {
print("No placemarks!")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2,通过地址获取经纬度
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController ,CLLocationManagerDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
locationEncode()
}
//地理信息编码
func locationEncode(){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString("南京市新街口大洋百货", completionHandler: {
(placemarks:[CLPlacemark]?, error:Error?) -> Void in
if error != nil {
self.textView.text = "错误:\(error!.localizedDescription))"
return
}
if let p = placemarks?[0]{
self.textView.text = "经度:\(p.location!.coordinate.longitude) "
+ "纬度:\(p.location!.coordinate.latitude)"
} else {
print("No placemarks!")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

我的位置定出来只有镇,怎么变成省,市,县,镇,...,这样的啊?
航哥,通过经纬度获取地址之后怎样上删除国家
geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
(placemarks:[AnyObject]!, error:NSError!) -> Void in
无法使用了,航哥