我有一个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是的,我已经尝试过与其他地点,它有时会显示一个地方或广场的名称(即城市中心北约克,梅尔伊士曼广场),但从来没有餐厅名称或商店名称(商业名称) –