iOS地图定位使用几个小功能(刚刚)
来源:互联网 发布:上海二二一数据怎么样 编辑:程序博客网 时间:2024/05/22 05:31
iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。
获取当前经纬度
首先导入#import <CoreLocation/CoreLocation.h>,定义CLLocationManager的实例,实现CLLocationManagerDelegate。
@interface ViewController ()<CLLocationManagerDelegate>{ CLLocationManager *_locationManager;}@end
开始定位的方法:
- (void)startLocating{ if([CLLocationManager locationServicesEnabled]) { _locationManager = [[CLLocationManager alloc] init]; //设置定位的精度 [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; _locationManager.distanceFilter = 100.0f; _locationManager.delegate = self; if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0) { [_locationManager requestAlwaysAuthorization]; [_locationManager requestWhenInUseAuthorization]; } //开始实时定位 [_locationManager startUpdatingLocation]; }}
实现代理方法:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"Longitude = %f", manager.location.coordinate.longitude); NSLog(@"Latitude = %f", manager.location.coordinate.latitude); [_locationManager stopUpdatingLocation];}
获取当前位置信息
在上面的代理方法中
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ NSLog(@"Longitude = %f", manager.location.coordinate.longitude); NSLog(@"Latitude = %f", manager.location.coordinate.latitude); [_locationManager stopUpdatingLocation]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; [geoCoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemarks, NSError *error) { for (CLPlacemark * placemark in placemarks) { NSDictionary *test = [placemark addressDictionary]; // Country(国家) State(城市) SubLocality(区) NSLog(@"%@", [test objectForKey:@"Country"]); NSLog(@"%@", [test objectForKey:@"State"]); NSLog(@"%@", [test objectForKey:@"SubLocality"]); NSLog(@"%@", [test objectForKey:@"Street"]); } }];}
这样就很简单获取了当前位置的详细信息。
获取某一个地点的经纬度
- (void)getLongitudeAndLatitudeWithCity:(NSString *)city{ //city可以为中文 NSString *oreillyAddress = city; CLGeocoder *myGeocoder = [[CLGeocoder alloc] init]; [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) { if ([placemarks count] > 0 && error == nil) { NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]); CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude); NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude); } else if ([placemarks count] == 0 && error == nil) { NSLog(@"Found no placemarks."); } else if (error != nil) { NSLog(@"An error occurred = %@", error); } }];}
计算两个地点之间的距离
- (double)distanceByLongitude:(double)longitude1 latitude:(double)latitude1 longitude:(double)longitude2 latitude:(double)latitude2{ CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1]; CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2]; double distance = [curLocation distanceFromLocation:otherLocation];//单位是m return distance;}
首先我们可以用上面的getLongitudeAndLatitudeWithCity方法获取某一个地点的经纬度。比如我们获取北京和上海的经纬度分别为:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之间的距离就是:
double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706];NSLog(@"Latitude = %f", distance);
计算的是大概的距离,可能没有那么精准。输入结果为:
distance = 1066449.749194
代码下载地址:MapDistanceMaster
0 0
- iOS地图定位使用几个小功能(刚刚)
- iOS地图定位使用几个小功能
- iOS地图定位使用几个小功能
- iOS地图定位小功能(上)
- iOS定位与地图功能
- iOS地图定位小理解
- iOS百度地图API的使用---定位功能
- IOS开发 地图定位 使用
- iOS使用系统地图定位
- 【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍
- iOS百度地图定位功能iOS8+
- ios系统地图的定位功能
- 百度地图的使用(2)_实现定位功能
- 使用百度地图api实现定位功能。
- 使用百度地图sdk实现定位功能
- 百度地图定位功能的使用
- iOS 百度地图 小功能demo
- ios使用地图定位记录运动轨迹
- 在Sql Server里通过Sql语句【连接】和【操作】另一个【外地数据库】
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- 在mac上通过Homebrew安装redis
- 我的Android进阶之旅------> Android应用升级构想和要点总结
- ios面试题积累
- iOS地图定位使用几个小功能(刚刚)
- Android访问网络,使用HttpURLConnection还是HttpClient?
- codevs1063
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- JVM常用分析工具JPS/JINFO/JSTACK/JSTAT/JMAP详解
- 表达式与条件组合
- stagefrightpalyer-----继承于MediaPlayerInterface
- 基于maven的openjpa简单例子
- VMware Vsphere高可用性(HA群集)