iOS 定位服务
2015-10-09 12:01
465 查看
首先在info.plist里面添加一行NSLocationWhenInUseUsageDescription的定位服务描述
#import "ViewController.h"
//#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<UIAlertViewDelegate,CLLocationManagerDelegate>
{
CLLocation *lastLocation;
UILabel *distanceLabel;
UILabel *timeLabel;
double allDistance;
int allTimeInterval;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判断用户是否打开了定位服务
if (![CLLocationManager locationServicesEnabled]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"打开定位服务" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
return;
}
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.frame), 40)];
distanceLabel.textAlignment = NSTextAlignmentCenter;
distanceLabel.textColor = [UIColor redColor];
[self.view addSubview:distanceLabel];
timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(distanceLabel.frame)+10, CGRectGetWidth(self.view.frame), 40)];
timeLabel.textAlignment = NSTextAlignmentCenter;
timeLabel.textColor = [UIColor redColor];
[self.view addSubview:timeLabel];
//
属性.语法
包含了 set
个体 方法
//
如果赋值
是 set 方法
//
调用 get
方法
[self.manager requestWhenInUseAuthorization];
[self.manager startUpdatingLocation];
// 通过地名查找地理位置(附近的位置)
// CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// [geocoder geocodeAddressString:@"贵阳" completionHandler:^(NSArray *placemarks, NSError *error)
{
// for (CLPlacemark *placeMark in placemarks) {
// NSLog(@"%@",placeMark.name);
// NSLog(@"%@",placeMark.country);
// NSLog(@"%@",placeMark.thoroughfare);
// NSLog(@"%@",placeMark.administrativeArea);
// }
// }];
//CLCircularRegion
区域范围
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 1) {
//
通过openURL的方式
跳到设置的页面
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
- (CLLocationManager *)manager
{
if (!_myManager) {
_myManager = [[CLLocationManager alloc]init];
_myManager.distanceFilter = 10;
_myManager.desiredAccuracy = kCLLocationAccuracyBest;
_myManager.delegate = self;
}
return _myManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@",locations);
CLLocation *curLocation = [locations lastObject];
if (curLocation.speed >= 30) {
NSLog(@"超速");
}
//
获得两次更新的间距
CLLocationDistance distance = [curLocation distanceFromLocation:lastLocation];
NSLog(@"获得两次更新的间距%f",distance);
if (lastLocation) {
//
用户驾驶了多远
//
计算上次更新的位置 和这次更新位置之间的距离
是两次位置更新之间的 行驶距离
//
用户驾驶了多远
把所有行驶距离 累加
//
计算两次位置更新之间的
行驶距离
// CLLocationDistance
单位是米
CLLocationDistance distance = [curLocation distanceFromLocation:lastLocation];
//
计算用户一共驾驶了多远
allDistance += distance;
distanceLabel.text = [NSString stringWithFormat:@"您现在已行驶了%d米",(int)allDistance];
//
计算用户行驶了多长时间
//
通过计算两次位置更新时间的间距 得到每一次更新距离的时间
//
把得到所有时间间隔
相加 就是用户行驶了多长时间
//
计算两次位置更新时间的间距
// NSTimeInterval(单位秒)
NSTimeInterval timeInterval = [curLocation.timestamp timeIntervalSinceDate:lastLocation.timestamp];
//
计算用户行驶的总时间
allTimeInterval += timeInterval;
int hour = allTimeInterval/(60*60);
//
把小数四舍五入lround()
int min = lround(allTimeInterval)%(60*60)/60;
int sec = lround(allTimeInterval)%60;
NSString *timeString = [NSString stringWithFormat:@"您已经驾驶了%02d:%02d:%02d",hour,min,sec];
timeLabel.text = timeString;
//
计算平均速度
float avgSpeed = allDistance/allTimeInterval;
NSLog(@"%0.2f",avgSpeed);
}
lastLocation = curLocation;
// 地理编码
把地理位置 转换成地名
// 定位到位置之后
停止定位
/* CLLocation *curLocation = [locations lastObject];
// 地理编码
反地理编码 在转换数据的时候
小号的时间比较长 用多线程比较好
//
进行地理编码
反地理编码的类 clgeocoder
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(@"%@",placemarks);
// for (CLPlacemark *placeMark in placemarks) {
// NSLog(@"%@",placeMark.name);
// NSLog(@"%@",placeMark.country);
// }
// }];
CLCircularRegion *region = [[CLCircularRegion alloc]initWithCenter:curLocation.coordinate radius:1000 identifier:@"..."];
[geocoder geocodeAddressString:@"务川" inRegion:region completionHandler:^(NSArray *placemarks, NSError
*error) {
NSLog(@"...%@",placemarks);
}];
*/
// [self.manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"未决定是否开启
位置服务!");
break;
}
case kCLAuthorizationStatusDenied: {
NSLog(@"用户拒绝
使用
位置服务 !");
break;
}
case kCLAuthorizationStatusAuthorizedAlways: {
NSLog(@"用户允许
使用
位置服务 !");
[self.manager startUpdatingLocation];
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse: {
NSLog(@"用户允许
使用
位置服务 !");
[self.manager startUpdatingLocation];
break;
}
default: {
break;
}
}
}
@end
#import "ViewController.h"
//#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<UIAlertViewDelegate,CLLocationManagerDelegate>
{
CLLocation *lastLocation;
UILabel *distanceLabel;
UILabel *timeLabel;
double allDistance;
int allTimeInterval;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判断用户是否打开了定位服务
if (![CLLocationManager locationServicesEnabled]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"打开定位服务" delegate:self
cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
return;
}
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, CGRectGetWidth(self.view.frame), 40)];
distanceLabel.textAlignment = NSTextAlignmentCenter;
distanceLabel.textColor = [UIColor redColor];
[self.view addSubview:distanceLabel];
timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(distanceLabel.frame)+10, CGRectGetWidth(self.view.frame), 40)];
timeLabel.textAlignment = NSTextAlignmentCenter;
timeLabel.textColor = [UIColor redColor];
[self.view addSubview:timeLabel];
//
属性.语法
包含了 set
个体 方法
//
如果赋值
是 set 方法
//
调用 get
方法
[self.manager requestWhenInUseAuthorization];
[self.manager startUpdatingLocation];
// 通过地名查找地理位置(附近的位置)
// CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// [geocoder geocodeAddressString:@"贵阳" completionHandler:^(NSArray *placemarks, NSError *error)
{
// for (CLPlacemark *placeMark in placemarks) {
// NSLog(@"%@",placeMark.name);
// NSLog(@"%@",placeMark.country);
// NSLog(@"%@",placeMark.thoroughfare);
// NSLog(@"%@",placeMark.administrativeArea);
// }
// }];
//CLCircularRegion
区域范围
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 1) {
//
通过openURL的方式
跳到设置的页面
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
- (CLLocationManager *)manager
{
if (!_myManager) {
_myManager = [[CLLocationManager alloc]init];
_myManager.distanceFilter = 10;
_myManager.desiredAccuracy = kCLLocationAccuracyBest;
_myManager.delegate = self;
}
return _myManager;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@",locations);
CLLocation *curLocation = [locations lastObject];
if (curLocation.speed >= 30) {
NSLog(@"超速");
}
//
获得两次更新的间距
CLLocationDistance distance = [curLocation distanceFromLocation:lastLocation];
NSLog(@"获得两次更新的间距%f",distance);
if (lastLocation) {
//
用户驾驶了多远
//
计算上次更新的位置 和这次更新位置之间的距离
是两次位置更新之间的 行驶距离
//
用户驾驶了多远
把所有行驶距离 累加
//
计算两次位置更新之间的
行驶距离
// CLLocationDistance
单位是米
CLLocationDistance distance = [curLocation distanceFromLocation:lastLocation];
//
计算用户一共驾驶了多远
allDistance += distance;
distanceLabel.text = [NSString stringWithFormat:@"您现在已行驶了%d米",(int)allDistance];
//
计算用户行驶了多长时间
//
通过计算两次位置更新时间的间距 得到每一次更新距离的时间
//
把得到所有时间间隔
相加 就是用户行驶了多长时间
//
计算两次位置更新时间的间距
// NSTimeInterval(单位秒)
NSTimeInterval timeInterval = [curLocation.timestamp timeIntervalSinceDate:lastLocation.timestamp];
//
计算用户行驶的总时间
allTimeInterval += timeInterval;
int hour = allTimeInterval/(60*60);
//
把小数四舍五入lround()
int min = lround(allTimeInterval)%(60*60)/60;
int sec = lround(allTimeInterval)%60;
NSString *timeString = [NSString stringWithFormat:@"您已经驾驶了%02d:%02d:%02d",hour,min,sec];
timeLabel.text = timeString;
//
计算平均速度
float avgSpeed = allDistance/allTimeInterval;
NSLog(@"%0.2f",avgSpeed);
}
lastLocation = curLocation;
// 地理编码
把地理位置 转换成地名
// 定位到位置之后
停止定位
/* CLLocation *curLocation = [locations lastObject];
// 地理编码
反地理编码 在转换数据的时候
小号的时间比较长 用多线程比较好
//
进行地理编码
反地理编码的类 clgeocoder
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
// [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(@"%@",placemarks);
// for (CLPlacemark *placeMark in placemarks) {
// NSLog(@"%@",placeMark.name);
// NSLog(@"%@",placeMark.country);
// }
// }];
CLCircularRegion *region = [[CLCircularRegion alloc]initWithCenter:curLocation.coordinate radius:1000 identifier:@"..."];
[geocoder geocodeAddressString:@"务川" inRegion:region completionHandler:^(NSArray *placemarks, NSError
*error) {
NSLog(@"...%@",placemarks);
}];
*/
// [self.manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"未决定是否开启
位置服务!");
break;
}
case kCLAuthorizationStatusDenied: {
NSLog(@"用户拒绝
使用
位置服务 !");
break;
}
case kCLAuthorizationStatusAuthorizedAlways: {
NSLog(@"用户允许
使用
位置服务 !");
[self.manager startUpdatingLocation];
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse: {
NSLog(@"用户允许
使用
位置服务 !");
[self.manager startUpdatingLocation];
break;
}
default: {
break;
}
}
}
@end
相关文章推荐
- iOS9AdaptationTips
- 项目适配iOS9遇到的一些问题及解决办法(更新两个小问题)
- iOS获取设备版本信息
- IOS中堆和栈的区别
- iOS开发MAC下配置svn
- iOS苹果官方Demo合集
- Xcode模拟器不能选择 强制打开提示iOS Simulator 意外退出
- iOS9.0修改状态栏颜色
- Xcode中添加.pch文件
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- 学习CGAffineTransform和CATransform3D
- IOS 捕获程序异常
- ios9:Search API
- iOS 9多任务分屏要点
- ios多线程简单整理
- IOS开发基础知识--碎片23
- 【转】iOS中属性与成员变量的区别
- 我的BIOS之行【番外】-dos操作系统
- 我的BIOS之行(2)-Aptio BIOS Overview
- iOS学习可用网址