私は自分のために小さな小さなアプリケーションを構築しています - 今、あなたのローカルエリア内で検索するperformSearchという機能がありますすべての近くのコーヒーの場所、ジム、レストランのそれぞれの場所でピンを落とす。しかし、実際のマップビューに表示されるように注釈を取得して場所の名前を表示する方法については混乱しています。誰でも経験はありますか?iOS逆ジオコーディング(アドレスだけでなく、座標からロケーションのNAMEを取得)
基本的には、代わりにアドレスだけを表示するので、私は「...スターバックス アドレスを」注釈が言いたい
サンプルコード:
これは、任意の検索フィールドで検索を行い、上のピンをドロップ指定されたエリアのすべての場所をその検索フィールドで地図表示します。
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.")
}
}
})
}