2011年4月12日 星期二

[轉]MKReverseGeocoder 的經緯度反查

from : http://www.furnace.idv.tw/


由於對於經緯度的感覺不是非常強烈,所以在很多時候經會需要使用到經緯度反查來確定目前的所在地,而 MapKit 就剛好有提供此項功能,在進行反查之前必須要先得到正確的經緯度,大家可以參閱使用 MapKit 取得目前地理位置與經緯度的設定一文,以下程式碼將示範如何進行經緯度的反查。
首先是在 ViewCotroller.h 中設定代理。 @interface LocationViewController : UIViewController { 設定好代理之後就可以使用反查的功能。 //初始化 myReverse = [[MKReverseGeocoder alloc] initWithCoordinate:mapCenter];  myReverse.delegate = self;  //開始反查 [myReverse start];
其中 myReverse 為 MKReverseGeocoder 型態的指標,而 mapCenter 為 CLLocationCoordinate2D 型態的變數,裡面紀錄著經緯度的位置。
在成功啟動反查功能之後可以由下列兩個內建函式得知結果。 反查失敗 - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error NSLog(@"Reverse Geocoder Errored"); }
反查成功 - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark NSLog(@"Reverse Geocoder completed");  //將查結果輸出到addressField中  addressField.text = [NSString stringWithFormat:@"%@, %@, %@", placemark.country, placemark.locality, placemark.thoroughfare]; } 在反查成功的函式中,會得到一個 MKPlacemark 型態的指標,裡面存放著許多反查之後的相關結果,這裡就不依序列出。

沒有留言:

張貼留言