1
我正在爲自己構建一個小型應用程序,現在它有一個名爲performSearch的函數,可以在您的本地區域內執行搜索所有附近的咖啡店,健身房和餐館都會在這些位置放置針腳。但是,我很困惑我如何獲得註解以顯示實際地圖視圖中顯示的位置名稱。任何人有任何經驗?iOS反向地理編碼(從座標獲取位置的名稱,而不僅僅是地址)
基本上,而不是隻顯示地址,我想註釋說「星巴克 地址......」
示例代碼:
這不會與任何給定的搜索字段搜索,並在降低引腳使用該搜索字段在給定區域中映射所有位置的視圖。
var matchingItems: [MKMapItem] = [MKMapItem]()
@IBOutlet weak var map: MKMapView!
func performSearch(searchField: String) {
matchingItems.removeAll()
//search request
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchField
request.region = self.map.region
// process the request
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler { response, error in
guard let response = response else {
print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)")
return
}
for item in response.mapItems {
// customize your annotations here, if you want
var annotation = item.placemark
self.reverseGeocoding(annotation.coordinate.latitude, longitude: allData.coordinate.longitude)
self.map.addAnnotation(annotation)
self.matchingItems.append(item)
}
}
}
func reverseGeocoding(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let location = CLLocation(latitude: latitude, longitude: longitude)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
if error != nil {
print(error)
return
}
else if placemarks?.count > 0 {
let pm = placemarks![0]
let address = ABCreateStringWithAddressDictionary(pm.addressDictionary!, false)
print("\n\(address)")
if pm.areasOfInterest?.count > 0 {
let areaOfInterest = pm.areasOfInterest?[0]
print(areaOfInterest!)
} else {
print("No area of interest found.")
}
}
})
}