iOS定位操作和经纬度距离计算
来源:互联网 发布:mac怎么设置动态壁纸 编辑:程序博客网 时间:2024/05/28 16:26
iOS的原声定位以来CoreLocation和MapKit框架
在iOS8以后执行定位操作需要在info.plst添加两个关键字段NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription如下图:
在需要定位的地方导入#import <CoreLocation/CoreLocation.h>
//开始定位
-(void)startLocation{
//判断定位操作是否被允许
if([CLLocationManagerlocationServicesEnabled]) {
self.locationManager = [[CLLocationManageralloc] init] ;
self.locationManager.delegate =self;
[self.locationManagerrequestAlwaysAuthorization];
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
[self.locationManagerstartUpdatingLocation];
}else {
//提示用户无法进行定位操作
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:
@"提示"message:@"定位不成功 ,请确认开启定位"delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
[alertView show];
}
}
//定位的回调方法,
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *oldLocation = locations[0];
CLLocationCoordinate2D oldCoordinate = oldLocation.coordinate;
DLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
[manager stopUpdatingLocation];//停止定位。
CLLocation* curLocation = [[CLLocationalloc] initWithLatitude:116.396566longitude:39.930309];
CLLocation* otherLocation = [[CLLocationalloc] initWithLatitude:116.394158longitude:39.929957];
double distance = [curLocationdistanceFromLocation:otherLocation];
NSLog(@"%f",distance);
//------------------位置反编码---5.0之后使用-----------------
CLGeocoder *geocoder = [[CLGeocoderalloc] init];
[geocoder reverseGeocodeLocation:oldLocation
completionHandler:^(NSArray *placemarks,NSError *error){
for (CLPlacemark *placein placemarks) {
// UILabel *label = (UILabel *)[self.window viewWithTag:101];
// label.text = place.name;
NSLog(@"name,%@",place.name); // 位置名
NSLog(@"thoroughfare,%@",place.thoroughfare); // 街道
NSLog(@"subThoroughfare,%@",place.subThoroughfare);// 子街道
NSLog(@"locality,%@",place.locality); // 市
NSLog(@"subLocality,%@",place.subLocality); // 区
NSLog(@"country,%@",place.country); // 国家
}
}];
}
警告:在模拟器定位需要给模拟器一个位置。
计算两个坐标点之间的距离
CLLocation* curLocation = [[CLLocationalloc] initWithLatitude:116.396566longitude:39.930309];
CLLocation* otherLocation = [[CLLocationalloc] initWithLatitude:116.394158longitude:39.929957];
double distance = [curLocationdistanceFromLocation:otherLocation];
默认单位为米。- iOS定位操作和经纬度距离计算
- iOS-计算两个经纬度之间的距离和方位
- 经纬度互换和计算距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS定位操作,获取当前位置,计算两点之间距离
- iOS, corelocation定位经纬度坐标,以及经纬度距离
- IOS根据两个经纬度计算相距距离
- IOS根据两个经纬度计算相距距离
- ios 根据两个经纬度计算距离
- iOS根据两个经纬度计算相距距离
- IOS根据两个经纬度计算相距距离
- IOS根据两个经纬度计算相距距离
- iOS根据2个经纬度计算距离
- iOS定位获取经纬度以及计算位置
- php根据经纬度计算距离和方向
- 2012年5月SAT香港真题解析
- 常见的面试问题及回答注意事项
- JavaScript 表单验证
- java 遍历泛型的方法
- FastDFS安装、配置、部署(三)-Storage配置详解
- iOS定位操作和经纬度距离计算
- python自然语言处理入门教程(一)
- [问题篇]VMWare搭建Openstack——Cinder创建扩展Volume的状态Error的问题
- python正则表达式
- 螺旋矩阵(算法)
- select,poll,epoll之间的区别
- 解决NSData中包含非法UTF-8编码
- 【贪心】【CTSC2007】【cogs1584】挂缀
- Android Studio启动显示Fetching Android SDK component information