根据经纬度翻译成详细位置的各种方法
首先苹果获取经纬度是
- if ([CLLocationManager locationServicesEnabled]) {//判断手机是否可以定位
- locationManager = [[CLLocationManager alloc] init];//初始化位置管理器
- [locationManager setDelegate:self];
- [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精度
- locationManager.distanceFilter = 1000.0f;//设置距离筛选器
- [locationManager startUpdatingLocation];//启动位置管理器
- }
然后根据其代理获取经纬度
- #pragma mark - CLLocationManager Delegate Methods
- - (void)locationManager:(CLLocationManager *)manager
- didUpdateToLocation:(CLLocation *)newLocation
- fromLocation:(CLLocation *)oldLocation;
- {
- location=[newLocation coordinate];//当前经纬度
- lat=location.latitude;
- lon=location.longitude;
- MKCoordinateSpan theSpan;
- theSpan.latitudeDelta=0.01f;
- theSpan.longitudeDelta=0.01f;
- theRegion.center=location;//定义地图显示范围
- theRegion.span=theSpan;
- }
根据经纬度解析成位置目前有2种。第一个是调用系统的CLGeocoder类的 reverseGeocodeLocation方法
- //根据经纬度解析成位置
- 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];
- } ];
第二种是调用google的api
----http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true
网上有人说系统的方法有时候解析不出来位置。第二种google,国内有时候访问不了google服务器,所以都有缺点。
有人建议国内的地图可以使用高德地图(以后摸索下)
PS1:网上有一个比较流行的查找附近功能,是根据google的api搜索的
#define SEARCH_GOOGLE @"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=1000&types=%@&sensor=true&key=AIzaSyALaqx0MfPsp2aldbZbzEQAq64SwgQfZ0c"
调用的话就是这样
jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"food"];
jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"bus_station"];
jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"hospital"];
PS2:有个功能,如果长时间按住地图上某个地方,如何获取其经纬度,进而对其显示位置呢?
----
- //开始点击长按,不然会执行2次。
- if (sender.state==UIGestureRecognizerStateBegan) {
- //isLongPress=YES;
- CGPoint touchPoint=[sender locationInView:self.myMapView];
- CLLocationCoordinate2D touchCoordinate=[self.myMapView convertPoint:touchPoint toCoordinateFromView:self.myMapView];//将触摸点转换经纬度
- //反向解析长按时的大头针的信息
- location =[[CLLocation alloc]initWithLatitude:touchCoordinate.latitude longitude:touchCoordinate.longitude];