我有一個mapview,我添加了一個方法在用戶按下的位置放置一個pin。標註顯示圖像上顯示的位置地址。我可以使用mapkit獲得商店名稱/餐廳名稱嗎?(swift)
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)
}
})
}
正如你可以看到,當我試圖通過調用線(獲取地點名稱(((如果讓LOCATIONNAME =標。 name)))),我只能得到地址:「5197 Yonge St」,而不是餐廳名稱:「Pho 88 Restaurant」。
任何人都可以告訴我我做錯了嗎?或者它根本無法實現?謝謝!
也許是因爲**地圖**的位置就是這樣嗎?不是**商業**地點? – dfd
您的代碼可以與其他位置完美配合,嘗試換一個位置。 –
@ArpitDongre是的,我已經嘗試過與其他地點,它有時會顯示一個地方或廣場的名稱(即城市中心北約克,梅爾伊士曼廣場),但從來沒有餐廳名稱或商店名稱(商業名稱) –