将坐标转换为城市名称? [英] Convert coordinates to City name?
问题描述
如何使用MapKit从坐标获取地址?
How to get an address from coordinates using MapKit?
当我在地图上长按它得到坐标时,我有这段代码:
I have this code when long press on the map it gets the coordinates:
func didLongPressMap(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.Began {
let touchPoint = sender.locationInView(self.mapView)
let touchCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
var annotation = MKPointAnnotation()
annotation.coordinate = touchCoordinate
annotation.title = "Your position"
self.mapView.addAnnotation(annotation) //drops the pin
println("lat: \(touchCoordinate.latitude)")
var num = (touchCoordinate.latitude as NSNumber).floatValue
var formatter = NSNumberFormatter()
formatter.maximumFractionDigits = 4
formatter.minimumFractionDigits = 4
var str = formatter.stringFromNumber(num)
println("long: \(touchCoordinate.longitude)")
var num1 = (touchCoordinate.longitude as NSNumber).floatValue
var formatter1 = NSNumberFormatter()
formatter1.maximumFractionDigits = 4
formatter1.minimumFractionDigits = 4
var str1 = formatter1.stringFromNumber(num1)
self.adressLoLa.text = "\(num),\(num1)"
}
}
我要打印 annotation.title
完整地址(街道,城市,邮编,国家)。
and I want to print in annotation.title
the complete address (street, city, zip, country).
推荐答案
SWIFT 4.0:编辑
MapKit
框架提供了一种从坐标获取地址详细信息的方法。
SWIFT 4.0 : EDIT
MapKit
framework does provide a way to get address details from coordinates.
您需要使用地图工具包的反向地理编码。 CLGeocoder
class用于从位置(坐标)获取地址和地址的位置。方法 reverseGeocodeLocation
将从坐标返回地址详细信息。
You need to use reverse geocoding of map kit. CLGeocoder
class is used to get the location from address and address from the location (coordinates). The method reverseGeocodeLocation
will returns the address details from coordinates.
此方法接受 CLLocation
作为参数并返回 CLPlacemark
,其中包含地址字典。
This method accepts CLLocation
as a parameter and returns CLPlacemark
, which contains address dictionary.
所以现在上面的方法将更新为:
So now above method will be updated as:
@objc func didLongPressMap(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let touchPoint = sender.location(in: mapView)
let touchCoordinate = mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = touchCoordinate
annotation.title = "Your position"
mapView.addAnnotation(annotation) //drops the pin
print("lat: \(touchCoordinate.latitude)")
let num = touchCoordinate.latitude as NSNumber
let formatter = NumberFormatter()
formatter.maximumFractionDigits = 4
formatter.minimumFractionDigits = 4
_ = formatter.string(from: num)
print("long: \(touchCoordinate.longitude)")
let num1 = touchCoordinate.longitude as NSNumber
let formatter1 = NumberFormatter()
formatter1.maximumFractionDigits = 4
formatter1.minimumFractionDigits = 4
_ = formatter1.string(from: num1)
self.adressLoLa.text = "\(num),\(num1)"
// Add below code to get address for touch coordinates.
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: touchCoordinate.latitude, longitude: touchCoordinate.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Location name
if let locationName = placeMark.location {
print(locationName)
}
// Street address
if let street = placeMark.thoroughfare {
print(street)
}
// City
if let city = placeMark.subAdministrativeArea {
print(city)
}
// Zip code
if let zip = placeMark.isoCountryCode {
print(zip)
}
// Country
if let country = placeMark.country {
print(country)
}
})
}
}
这篇关于将坐标转换为城市名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!