iOS定位服务与地图开发(3)---地理信息编码查询
生活随笔
收集整理的這篇文章主要介紹了
iOS定位服务与地图开发(3)---地理信息编码查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
即根據一個NSString的文字描述對象獲取相關的地理坐標。
采用CLGeocoder類操作,具體方法:
1>geocodeAddressDictionary:completionHandler: 通過指定一個地址信息字典對象參數進行查詢
2>geocodeAddressString:completionHandler:通過指定一個地址信息字符串參數進行查詢
3>geocodeAddressString:inRegion:completionHandler:通過制定地址信息字符串和查詢范圍進行查詢
- (IBAction)geocodeQuery:(id)sender {NSString *queryStr = nil;// 從界面文本框輸入地址字符串if (_textField.text == nil || [_textField.text length] == 0) {return ;}CLGeocoder *geocoder = [[CLGeocoder alloc] init];[geocoder geocodeAddressString:queryStr completionHandler:^(NSArray *placemarks, NSError *error) {NSLog(@"查詢記錄數:%i",[placemarks count]);if ([placemarks count] > 0) {CLPlacemark *placemark = [placemarks objectAtIndex:0];CLLocationCoordinate2D coordinate = placemark.location.coordinate;// 生成經緯度字符串NSString *strCoordinate = [NSString stringWithFormat:@"經度:%3.5f,緯度:%3.5f",coordinate.latitude,coordinate.longitude];NSDictionary *addressDict = placemark.addressDictionary;NSString *address = [addressDict objectForKey:(NSString *)kABPersonAddressStreetKey];address = address == nil ? @"" : address;NSString *state = [addressDict objectForKey:(NSString *)kABPersonAddressStateKey];state = state == nil ? @"" : state;NSString *city = [addressDict objectForKey:(NSString *)kABPersonAddressCityKey];city = city == nil ? @"" : city;// 獲取地標對應的可讀的文字信息,一般返回到UI顯示NSString *streetInfo = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n",strCoordinate,state,address,city];}}]; }使用geocodeAddressString:inRegion:completionHandler:指定查詢區域:
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:_currLocation.coordinate radius:1000.0f identifier:@"GeocodeRegion"];CLGeocoder *geocoder = [[CLGeocoder alloc] init];[geocoder geocodeAddressString:queryStr inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) {.......}];CLRegion對象封裝了一個地理區域類。
構造方法參數:
center:指定區域中心點
radius:指定區域半徑,單位為米
identifier:為區域指定一個標識
?
轉載于:https://www.cnblogs.com/yaoxc/p/3721799.html
總結
以上是生活随笔為你收集整理的iOS定位服务与地图开发(3)---地理信息编码查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .jardesc文件
- 下一篇: Loaders