OS定位操作,获取当前位置,计算两点之间距离
2014-02-27 13:36
302 查看
一、导入CoreLocation.framework
二、#import <CoreLocation/CoreLocation.h>
三、声明代理 <CLLocationManagerDelegate>
四、代码实现
1、声明
2、更新位置后代理方法,iOS6.0一下的方法
3、iOS6.0以上苹果的推荐方法
4、更新失败的方法
五、根据两点坐标计算两点之间的距离,此方法为苹果自带方法,亲测速度比高德API速度快很多,但是数据与高德API得到的不一样,准确度本人未能证实
二、#import <CoreLocation/CoreLocation.h>
三、声明代理 <CLLocationManagerDelegate>
四、代码实现
1、声明
01 | CLLocationManager *locationManager; //定义Manager |
02 | // 判断定位操作是否被允许 |
03 | if ([CLLocationManager locationServicesEnabled]) { |
04 | CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; |
05 |
06 | self.locationManager.delegate = self; |
07 | } else { |
08 | //提示用户无法进行定位操作 |
09 | } |
10 |
11 | // 开始定位 |
12 | [locationManager startUpdatingLocation]; |
01 | - ( void )locationManager:(CLLocationManager *)manager |
02 | didUpdateToLocation:(CLLocation *)newLocation |
03 | fromLocation:(CLLocation *)oldLocation { |
04 |
05 | //latitude和lontitude均为NSString型变量 |
06 | //纬度 |
07 | self.latitude = [NSStringstringWithFormat:@ "%.4f" , newLocation.coordinate.latitude]; |
08 |
09 | //经度 |
10 | self.longitude = [NSString stringWithFormat:@ "%.4f" , |
11 |
12 | } |
01 | -( void )locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations |
02 | { |
03 | //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation |
04 | CLLocation *currentLocation = [locations lastObject]; |
05 |
06 | CLLocationCoordinate2D coor = currentLocation.coordinate; |
07 | self.latitude =coor.latitude; |
08 | self.longitude = coor.longitude; |
09 |
10 | //[self.locationManager stopUpdatingLocation]; |
11 |
12 | } |
1 | - ( void )locationManager:(CLLocationManager *)manager |
2 | didFailWithError:(NSError *)error { |
3 |
4 | if (error.code == kCLErrorDenied) { |
5 | // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 |
6 | } |
7 | } |
1 | //第一个坐标 |
2 | CLLocation *current=[[CLLocation alloc] initWithLatitude:32.178722 longitude:119.508619]; |
3 | //第二个坐标 |
4 | CLLocation *before=[[CLLocation alloc] initWithLatitude:32.206340 longitude:119.425600]; |
5 | // 计算距离 |
6 | CLLocationDistance meters=[current distanceFromLocation:before]; |
相关文章推荐
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- 获取当前位置,计算两点之间的距离
- 获取指定经纬度及当前位置经纬度,并计算两个位置之间的距离
- iOS定位操作,获取当前位置
- ios 利用自带的CoreLocation.framework 实现获取手机当前处于的精度和纬度 并计算两点之间的间距(特别使用于团购,附近商户之类的项目)
- h5获取经纬度,当前位置,并计算与另一个经纬度的距离 千米为单位
- 百度地图最新版本sdk使用系列(八)-获取当前地图中心点的经纬度并计算与当前位置的距离
- Windows 8 地理位置定位 4.根据经纬度计算地面两点间的距离
- 最新js获取经纬度并计算两个位置之间的距离
- java使用geocoder,获取经纬度并计算没两点间距离
- mysql 下 计算 两点 经纬度 之间的距离 含具体sql语句
- 怎么根据经维度计算两点之间的距离,怎么根据经纬度与距离计算目标地点的经纬度
- Delphi 根据经纬度计算地球上两点之间的距离
- 百度地图如何计算两点之间距离
- 利用JS实现的根据经纬度计算地球上两点之间的距离
- 地图-获取两点之间的距离(经纬度)