【发布时间】:2017-03-03 04:24:37
【问题描述】:
我有一个地图视图,并且我添加了一种方法来在用户按下的位置放置一个图钉。如图所示,标注显示了该位置的地址。
screenshot of my mapview with annotation pin and callout view.
我的代码如下:
func onTapGestureRecognized(sender: UILongPressGestureRecognizer) {
self.mapView.removeAnnotations(mapView.annotations)
let location = tapRecognizer.location(in: mapView)
let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
let getLat: CLLocationDegrees = coordinate.latitude
let getLon: CLLocationDegrees = coordinate.longitude
let theLocation: CLLocation = CLLocation(latitude: getLat, longitude: getLon)
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(theLocation, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
var theLocationName = ""
var theStreetNumber = ""
var theStreet = ""
var theCity = ""
var theZip = ""
var theCountry = ""
// Address dictionary
print(placeMark.addressDictionary as Any)
// Location name
if let locationName = placeMark.name{
theLocationName = locationName
}
if let streetNumber = placeMark.subThoroughfare{
theStreetNumber = streetNumber
}
// Street address
if let street = placeMark.thoroughfare {
theStreet = street
}
// City
if let city = placeMark.locality {
theCity = city
}
// Zip code
if let zip = placeMark.postalCode{
theZip = zip
}
// Country
if let country = placeMark.isoCountryCode{
theCountry = country
}
let annotation = MKPointAnnotation()
annotation.title = theLocationName
annotation.subtitle = theStreetNumber + " " + theStreet + ", " + theCity + ", " + theCountry + ", " + theZip
if let location = placeMark.location {
annotation.coordinate = location.coordinate
// Display the annotation
self.mapView.showAnnotations([annotation], animated: true)
}
})
}
如您所见,当我尝试通过调用该行 (((( if let locationName = placeMark.name )))) 来获取位置名称时,我只能获取地址:“5197 Yonge St”,而不是餐厅名称:“Pho 88 餐厅”。
谁能告诉我哪里做错了?还是根本无法实现?谢谢!
【问题讨论】:
-
可能是因为地图位置就是这样?不是商家位置?
-
您的代码与其他位置完美配合,请尝试其他位置。
-
@ArpitDongre 是的,我尝试过其他位置,它有时会显示一个地方或广场的名称(即北约克市中心,梅尔伊士曼广场),但从来没有餐厅名称或商店名称(企业名称)
-
@dfd,嗯,如果是这个原因,你知道我怎样才能获得营业地点吗?
标签: ios swift mapkit core-location mkannotation