您的位置:首页 > 移动开发 > Objective-C

定位CoreLocation的基本使用

2015-12-13 22:47 597 查看
首先导入主头文件:

#import <CoreLocation/CoreLocation.h>

CoreLocation中使用CLLocationManager对象来做用户定位

CLLocationManager可以调用下面的方法进行开始定位和停止定位:
开始用户定位:
- (void)startUpdatingLocation;
停止用户定位:

- (void)stopUpdatingLocation;
当开启用户定位后就会不停地调用下面方法(代理方法),要想调用下面方法首先要实现代理:CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;


参数locations是个数组,里面存放CLLocation类型的对象。

    CLLocation *location = [locationslastObject];   // 获取location对象
当用户对应用地理位置的权限做出改变的时候会调用:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
参数status就是当前授权的状态。可以根据不同的授权状态去进行不同的操作。

在使用定位服务前需要用户进行授权:(需要在info.plist中设置)



1、总是使用用户位置:NSLocationAlwaysUsageDescription。
2、使用应用时定位:NSLocationWhenInUseDescription。

CLGeocoder类的使用(可以完成“地理编码”和“反地理编码”)。
1、地理编码方法(给出地理位置addressString,可以获得CLLocation对象)

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
NSString *address = "北京";    
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
// 1.如果解析有错误,或者解析出的数组个数为0.直接返回。placemarks里面装着CLPlacemark对象

if (placemarks.count == 0 || error) return;

// 2.遍历所有的地标对象
for (CLPlacemark *pm in placemarks) {  // 在这里placemarks.count == 1;

// 2.1.取出位置信息
CLLocation *location = pm.location;
// 2.2.取出经纬度
CLLocationCoordinate2D coordinate = location.coordinate;
}
}];
}

2、反地理编码(这个应该比较常用,根据你当前的位置获取当前城市或者你的具体地点。或者说就是把你所在的经纬度转换成你所在地点的名称)

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.floatValue longitude:longitude.floatValue];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// 如果有错误,或者解析出来的地址数量为0
if (placemarks.count == 0 || error) return ;

// 取出地标,就可以取出地址信息,以及CLLocation对象
CLPlacemark *pm = [placemarks firstObject];

NSLog(@"%@", pm.locality); // 打印当前所在城市的名称
}];
上面的location,可以用上面的方法(如下)获取当前用户的location,这样就可以获得当前用户所在的地理位置信息。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations;


再介绍下CLPlacemark这个类的一些属性:

//        @property (nonatomic, readonly, copy, nullable) NSString *name; //详细地址 如:中国北京市朝阳区奥运村街道北中轴景观大道
//        @property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; //街道名称 如:北中轴景观大道
//        @property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; //精确到具体几号街道
//        @property (nonatomic, readonly, copy, nullable) NSString *locality; //城市名 如:北京市
//        @property (nonatomic, readonly, copy, nullable) NSString *subLocality; //区或者县 如:朝阳区
//        @property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; //省或者直辖市如:北京市
//        @property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // county, eg. Santa Clara
//        @property (nonatomic, readonly, copy, nullable) NSString *postalCode; // zip code, eg. 95014
//        @property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; //如:CN
//        @property (nonatomic, readonly, copy, nullable) NSString *country; //如:中国
//        @property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // eg. Lake Tahoe
//        @property (nonatomic, readonly, copy, nullable) NSString *ocean; // eg. Pacific Ocean

通过获得用户的地理位置信息就可以做很多拓展操作了:比如做一个天气App。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息