即根据一个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:为区域指定一个标识