系统自带的方法:
//根据经纬度解析成位置
- CLGeocoder *geocoder=[[[CLGeocoder alloc]init]autorelease];
- [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark,NSError *error)
- {
- CLPlacemark *mark=[placemark objectAtIndex:0];
- place.title=@"没有当前位置的详细信息";
- place.subTitle=@"详细信息请点击‘附近’查看";
- place.title=[NSString stringWithFormat:@"%@%@%@",mark.subLocality,mark.thoroughfare,mark.subThoroughfare];
- place.subTitle=[NSString stringWithFormat:@"%@",mark.name];//获取subtitle的信息
- [self.myMapView selectAnnotation:place animated:YES];
- } ];
// 百度地图反编译
-(void)mapViewWillStartLocatingUser:(BMKMapView *)mapView{
NSLog(@"开始定位");
}
/**
*用户位置更新后,会调用此函数
*@param mapView 地图View
*@param userLocation 新的用户位置
*/
-(void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation{
NSLog(@"latitude--%f,longtitude---%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
locaLatitude=userLocation.location.coordinate.latitude;//纬度
locaLongitude=userLocation.location.coordinate.longitude;//精度
BMKCoordinateRegion region;
//将定位的点居中显示
region.center.latitude=locaLatitude;
region.center.longitude=locaLongitude;
//反地理编码出地理位置
CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
pt=(CLLocationCoordinate2D){locaLatitude,locaLongitude};
BOOL flag=[_search reverseGeocode:pt];
if (flag) {
myMapView.showsUserLocation=NO;//不显示自己的位置
self.btnDone.enabled=YES;
}
//当前位置标注和地图的比例(注释为可不用)
// BMKCoordinateSpan spans;
// spans.latitudeDelta=0.01;
// spans.longitudeDelta=0.01;
// region.span=spans;
myMapView.region=region;
}
在使用 [_search reverseGeocode:pt] 的时候,会调用它的一个协议方法,也就是下面的方法:
//反地理编码 -(void)onGetAddrResult:(BMKAddrInfo *)result errorCode:(int)error{ if (error==0) { BMKPointAnnotation *item=[[BMKPointAnnotation alloc] init]; item.coordinate=result.geoPt;//地理坐标 item.title=result.strAddr;//地理名称 [myMapView addAnnotation:item]; myMapView.centerCoordinate=result.geoPt; self.lalAddress.text=[result.strAddr stringByReplacingOccurrencesOfString:@"-" withString:@""]; if (![self.lalAddress.text isEqualToString:@""]) { strProvince=result.addressComponent.province;//省份 strCity=result.addressComponent.city;//城市 strDistrict=result.addressComponent.district;//地区 } // CLGeocoder *geocoder=[[CLGeocoder alloc] init]; // CLGeocodeCompletionHandler handle=^(NSArray *palce,NSError *error){ // for (CLPlacemark *placemark in palce) { // NSLog(@"%@1-%@2-%@3-%@4-%@5-%@6-%@7-%@8-%@9-%@10-%@11-%@12",placemark.name,placemark.thoroughfare,placemark.subThoroughfare,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.postalCode,placemark.ISOcountryCode,placemark.country,placemark.inlandWater,placemark.ocean,placemark.areasOfInterest); // break; // } // }; // CLLocation *loc = [[CLLocation alloc] initWithLatitude:locaLatitude longitude:locaLongitude]; // [geocoder reverseGeocodeLocation:loc completionHandler:handle]; } }