iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。
其定位有3种方式:
1,GPS,最精确的定位方式
2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。
3,Wifi,这种方式貌似是通过网络运营商的数据库得到的数据,在3种定位种最不精确
首先你要在你的Xcode中添加两个连接库,MapKit和CoreLocation,如图
core location提供了定位功能,能定位装置的当前坐标,同时能得到装置移动信息,最重要的类是CLLocationManager,定位管理。
iOS8开始,Core Location framework的变化主要有以下几点:
1. 在定位状态中引入Always 和WhenInUse的概念。
2. 加入Visit monitoring的特性, 这类特性特别适合旅行类别的应用,当用户到达某个指定的区域内,monitor开始作用。
3.加入室内定位技术,增加CLFloor, 在室内可以得到楼层信息。
获取当前经纬度
首先导入#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; }
double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706]; NSLog(@"Latitude = %f", distance);
distance = 1066449.749194
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。