iOS开发笔记(1) 根据经纬度翻译成详细位置的各种方法



http://blog.csdn.net/xiaofansong/article/details/8185498



首先苹果获取经纬度是

  1. if ([CLLocationManager locationServicesEnabled]) {//判断手机是否可以定位  
  2.     locationManager = [[CLLocationManager alloc] init];//初始化位置管理器  
  3.     [locationManager setDelegate:self];  
  4.     [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精度  
  5.     locationManager.distanceFilter = 1000.0f;//设置距离筛选器  
  6.     [locationManager startUpdatingLocation];//启动位置管理器  
  7. }  
    if ([CLLocationManager locationServicesEnabled]) {//判断手机是否可以定位
        locationManager = [[CLLocationManager alloc] init];//初始化位置管理器
        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精度
        locationManager.distanceFilter = 1000.0f;//设置距离筛选器
        [locationManager startUpdatingLocation];//启动位置管理器
    }

然后根据其代理获取经纬度

  1. #pragma mark - CLLocationManager Delegate Methods  
  2. - (void)locationManager:(CLLocationManager *)manager  
  3.     didUpdateToLocation:(CLLocation *)newLocation  
  4.            fromLocation:(CLLocation *)oldLocation;  
  5. {  
  6.     location=[newLocation coordinate];//当前经纬度  
  7.     lat=location.latitude;  
  8.     lon=location.longitude;  
  9.       
  10.     MKCoordinateSpan theSpan;  
  11.     theSpan.latitudeDelta=0.01f;  
  12.     theSpan.longitudeDelta=0.01f;  
  13.     theRegion.center=location;//定义地图显示范围  
  14.     theRegion.span=theSpan;  
  15. }  
#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方法

  1. //根据经纬度解析成位置  
  2.    CLGeocoder *geocoder=[[[CLGeocoder alloc]init]autorelease];  
  3.    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark,NSError *error)  
  4.     {  
  5.         CLPlacemark *mark=[placemark objectAtIndex:0];  
  6.         place.title=@"没有当前位置的详细信息";  
  7.         place.subTitle=@"详细信息请点击‘附近’查看";  
  8.         place.title=[NSString stringWithFormat:@"%@%@%@",mark.subLocality,mark.thoroughfare,mark.subThoroughfare];  
  9.         place.subTitle=[NSString stringWithFormat:@"%@",mark.name];//获取subtitle的信息  
  10.         [self.myMapView selectAnnotation:place animated:YES];  
  11.     } ];  
 //根据经纬度解析成位置
    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:有个功能,如果长时间按住地图上某个地方,如何获取其经纬度,进而对其显示位置呢?

----

  1. //开始点击长按,不然会执行2次。  
  2. if (sender.state==UIGestureRecognizerStateBegan) {  
  3.     //isLongPress=YES;  
  4.     CGPoint touchPoint=[sender locationInView:self.myMapView];  
  5.     CLLocationCoordinate2D touchCoordinate=[self.myMapView convertPoint:touchPoint toCoordinateFromView:self.myMapView];//将触摸点转换经纬度  
  6.     //反向解析长按时的大头针的信息  
  7.     location =[[CLLocation alloc]initWithLatitude:touchCoordinate.latitude longitude:touchCoordinate.longitude];  
    //开始点击长按,不然会执行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];
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值