免費開源的iOS開發學習平臺

                                                定位服務:5-CLGeocoder應用案例2

                                                上一節的示例代碼演示了使用CLGeocoder類進行地址到經緯度的轉換,本節我們來完成從經緯度到地址的反向轉換過程。

                                                準備工作

                                                新建一個Single View Application工程,并導入CoreLocation框架,操作方法參見前面章節。

                                                在Storyboard中,搭建如下的界面,用于輸入經緯度信息,并且可以顯示對應的地址信息。建立控件與控制器類之間的連線,添加屬性以及按鈕的點擊方法。

                                                在控制器類中,添加一個CLGeocoder類的屬性,并對其進行簡單的初始化操作。

                                                #import <CoreLocation/CoreLocation.h>
                                                @interface ViewController ()
                                                @property (weak, nonatomic) IBOutlet UITextField *lngTextField;//經度
                                                @property (weak, nonatomic) IBOutlet UITextField *latTextField;//緯度
                                                @property (weak, nonatomic) IBOutlet UITextField *addressTextField;//地址
                                                @property (weak, nonatomic) IBOutlet UITextField *nameTextField;//名稱
                                                @property (nonatomic,strong)CLGeocoder *geocoder;
                                                @end
                                                
                                                - (CLGeocoder *)geocoder {
                                                    if (_geocoder == nil) {
                                                        _geocoder = [[CLGeocoder alloc] init];
                                                    }
                                                    return _geocoder;
                                                }
                                                

                                                實現經緯度翻譯功能

                                                在【獲取經緯度的地址名稱】按鈕的點擊方法中,添加如下代碼,實現根據用戶輸入的經緯度獲取該經緯度對應的地址名稱,并顯示到界面上。

                                                - (IBAction)reverseGeocode:(id)sender {
                                                    double latitude  = [self.latTextField.text doubleValue];
                                                    double longitude = [self.lngTextField.text doubleValue];
                                                    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
                                                    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                                                        if ([placemarks count] > 0) {
                                                            //返回描述信息
                                                            CLPlacemark *placemark = placemarks[0];
                                                            //獲取所需信息
                                                            NSString *country = placemark.country;
                                                            NSString *area = placemark.administrativeArea;
                                                            NSString *city = placemark.locality;
                                                            NSString *street = placemark.thoroughfare;
                                                            //在textView上面顯示
                                                            self.addressTextField.text = [NSString stringWithFormat:@"%@ %@ %@ %@",country,area,city,street];
                                                            self.nameTextField.text = placemark.name;
                                                        }
                                                    }];
                                                }
                                                

                                                運行效果如下所示,當我們輸入經緯度信息后,點擊按鈕,即可顯示該經緯度對應的地址信息。

                                                示例代碼

                                                https://github.com/99ios/17.2.5


                                                欧美超级精品一级A片|欧美一级黄色视频|国产精品自在在线午夜免费|4444亚洲人成无码网在线观看|亚洲ⅴa曰本va欧美va视频