iOS获取到用户当前位置
2016-09-10 16:24
609 查看
通过CoreLocation定位,获取到用户当前位置,跟地图中的定位不同。
一、导入CoreLocation.framework
二、#import <CoreLocation/CoreLocation.h>
三、声明代理 <CLLocationManagerDelegate>
四、代码实现
1、声明
CLLocationManager *locationManager;//定义Manager // 判断定位操作是否被允许 if([CLLocationManager locationServicesEnabled]) { CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; self.locationManager.delegate = self; }else { //提示用户无法进行定位操作 } // 开始定位 [locationManager startUpdatingLocation];
2、更新位置后代理方法,iOS6.0一下的方法
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //latitude和lontitude均为NSString型变量 //纬度 self.latitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.latitude]; //经度 self.longitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.longitude]; }
3、iOS6.0以上苹果的推荐方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation CLLocation *currentLocation = [locations lastObject]; CLLocationCoordinate2D coor = currentLocation.coordinate; self.latitude = coor.latitude; self.longitude = coor.longitude; //[self.locationManager stopUpdatingLocation]; }
4、更新失败的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- iOS下获取用户当前位置的信息
- iOS开发笔记(使用Google Map Service获取用户当前位置信息)
- IOS获取当前地理位置文本
- 获取用户的当前位置的步骤
- IOS获取当前位置
- ios 定位获取当前位置信息
- 在iOS开发中自动获取当前的位置(GPS定位)
- iOS基于百度地图的开发(1)——用户当前位置
- iOS定位操作,获取当前位置,计算两点之间距离
- 使用新浪的接口获取用户当前IP的位置地点身份城市地区
- IOS获取当前地理位置文本
- 如何获取用户手机当前是否启动了android机的模拟位置功能
- iOS获取用户位置
- IOS开发实例-获取用户当前地理坐标
- ios 实现获取当前地理位置
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- iOS获取用户位置(续)
- iOS百度地图——用户当前位置
- IOS开发实例-获取用户当前地理坐标
- 在iOS开发中自动获取当前的位置(GPS定位)