通过CLLocationManager定位当前坐标和城市

IOS中的core location提供了定位功能,能定位装置的当前坐标,同时能得到装置移动信息。因为对定位装置的轮询是很耗电的,所以最好只在非常必要的前提下启动。

其中,最重要的类是CLLocationManager,定位管理。

其定位有3种方式:

1,GPS,最精确的定位方式,貌似iphone1是不支持的。

2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。

3,Wifi,这种方式貌似是通过网络运营商的数据库得到的数据,在3种定位种最不精确


使用方式:

1,引入CoreLocation的包,一般的默认模板里是没有的,所以需要手动导入。

2,通过启动CLLocationManager来启动定位服务,因为定位信息是需要轮询的,而且对于程序来说是需要一定时间才会得到的,所以翠玉lcationManager的操作大多都给委托来完成。

加载locationManager的代码:

 

  1. CLLocationManager *locationManager [[CLLocationManager alloc] init];//创建位置管理器  
  2.  locationManager.delegate=self;  
  3. locationManager.desiredAccuracy=kCLLocationAccuracyBest;  
  4. locationManager.distanceFilter=1000.0f;  
  5. //启动位置更新  
  6. [locationManager startUpdatingLocation];  

CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器 locationManager.delegate=self; locationManager.desiredAccuracy=kCLLocationAccuracyBest; locationManager.distanceFilter=1000.0f; //启动位置更新 [locationManager startUpdatingLocation];desiredAccuracy为设置定位的精度,可以设为最优,装置会自动用最精确的方式去定位。

 

distanceFilter是距离过滤器,为了减少对定位装置的轮询次数,位置的改变不会每次都去通知委托,而是在移动了足够的距离时才通知委托程序,它的单位是米,这里设置为至少移动1000再通知委托处理更新。

startUpdatingLocation就是启动定位管理了,一般来说,在不需要更新定位时最好关闭它,用stopUpdatingLocation,可以节省电量。


对于委托CLLocationManagerDelegate,最常用的方法是:

 

-(void)locationManager:(CLLocationManager *)manager

   didUpdateToLocation:(CLLocation *)newLocation

          fromLocation:(CLLocation *)oldLocation

{

//    当前的经度

    NSString *currentLatitude = [[NSString alloc

                                 initWithFormat:@"%g"

                                 newLocation.coordinate.latitude];

    latitude.text = currentLatitude;

    

//    当前的纬度

    NSString *currentLongitude = [[NSString alloc

                                  initWithFormat:@"%g",

                                  newLocation.coordinate.longitude];

    longitude.text = currentLongitude;

    

//    当前的水平距离

    NSString *currentHorizontalAccuracy = 

    [[NSString alloc

     initWithFormat:@"%g",

     newLocation.horizontalAccuracy];

    horizontalAccuracy.text = currentHorizontalAccuracy;

    

    NSString *currentAltitude = [[NSString alloc

                                 initWithFormat:@"%g",                                                          

                                 newLocation.altitude];

    altitude.text = currentAltitude;

    

    NSString *currentVerticalAccuracy = 

    [[NSString alloc

     initWithFormat:@"%g",

     newLocation.verticalAccuracy];

    verticalAccuracy.text = currentVerticalAccuracy;

    

    if (startLocation == nil)

        self.startLocation = newLocation;

    

    CLLocationDistance distanceBetween = [newLocation

                                          distanceFromLocation:startLocation];

    

    NSString *tripString = [[NSString alloc

                            initWithFormat:@"%f"

                            distanceBetween];

    distance.text = tripString;   


    //定位城市通过CLGeocoder

    CLGeocoder * geoCoder = [[CLGeocoder allocinit];

    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        for (CLPlacemark * placemark in placemarks) {

            

            NSString *test = [placemark locality];

            NSLog(@"%@", test);

            self.myCity.text = [NSString stringWithFormat:@"%@",placemark];

        }    

    }];

}

这个方法即定位改变时委托会执行的方法。

可以得到新位置,旧位置,CLLocation里面有经度纬度的坐标值,

同时CLLocation还有个属性horizontalAccuracy,用来得到水平上的精确度,它的大小就是定位精度的半径,单位为米。

如果值为-1,则说明此定位不可信。


另外委托还有一个常用方法是

- (void)locationManager:(CLLocationManager *)manager   
       didFailWithError:(NSError *)error ;

当定位出现错误时就会调用这个方法。

关于coreLocation - 地理位置反向编码

CoreLocation中得到的定位信息都是以经度和纬度等表示的地理信息,很多时候我们需要把它反向编码成普通人能读懂的地理位置描述如:X国XX市XXX区XXX街道XX号,这就需要用到MapKit中的一个地理位置反向编码工具:MKReverseGeocoder,


用法:

1,首先要实现协议MKReverseGeocoderDelegate,因为将坐标信息发到服务器再反回来需要一定的时间,所以为了防止阻塞,发出信息后并不知到什么时候会返回信息,信息返回时会通知委托方法。这里实现这个类主要时为了实现2个方法如下

 

  1. (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{  
  2.     NSLog(@"MKReverseGeocoder has failed.");  
  3.  
  4. (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{  
  5.       
  6.     NSLog(@"当前地理信息为:%@",placemark);  
  7.  

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ NSLog(@"MKReverseGeocoder has failed.");} - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{ NSLog(@"当前地理信息为:%@",placemark); }didFailWithError这个方法是来处理返回错误信息的,didFindPlacemark则是地理信息返回了,地理信息包含在placemark里面,此对象中包含国家,城市,区块,街道等成员变量。

 


2,然后可以init一个反向编码器,然后发出请求了:

 

  1. MKReverseGeocoder *reverseGeocoder =[[MKReverseGeocoder alloc] initWithCoordinate:coordinate];  
  2.     NSLog(@"%g",coordinate.latitude);  
  3.     NSLog(@"%g",coordinate.longitude);  
  4.     reverseGeocoder.delegate self;  
  5.     [reverseGeocoder start];  

MKReverseGeocoder *reverseGeocoder =[[MKReverseGeocoder alloc] initWithCoordinate:coordinate]; NSLog(@"%g",coordinate.latitude); NSLog(@"%g",coordinate.longitude); reverseGeocoder.delegate = self; [reverseGeocoder start];MKReverseGeocoder除了start方法,还有cancel方法来取消请求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值