iOS苹果的定位
2016-04-05 17:14
134 查看
苹果的定位是通过CoreLocation模块来实现的。
以下是一个定位的Demo。
//
// AppleLocationSingle.m
// bk
//
// Created by jie on 16/4/5.
// Copyright © 2016年 jie. All rights reserved.
//
#import "AppleLocationSingle.h"
#import <UIKit/UIKit.h>
@implementation AppleLocationSingle
//定位管理的懒加载
-(CLLocationManager *)locationManger{
if (!_locationManger) {
_locationManger=[[CLLocationManager
alloc]init];
_locationManger.delegate=self;
_locationManger.desiredAccuracy =
kCLLocationAccuracyBest;
/*
当设置为最优的时候,苹果会根据你的精度distanceFilter大小来决定最好的方式满足你的精度。GPS最精确,其次是蜂窝基站三角定位,然后是Wifi通过网络运营商的数据库得到的数据
*/
_locationManger.distanceFilter =
20;//一般是能控制在20米的精度(当系统检测到你的位置没有远离上个位置20米时是不会调取你的代理)
[_locationManger requestAlwaysAuthorization];//处于后台的情况下也可以定位。
[_locationManger
startUpdatingLocation];
}
//判断用户是否允许当前程序使用定位服务
if ([CLLocationManager
authorizationStatus]==kCLAuthorizationStatusNotDetermined || [CLLocationManager
authorizationStatus]==kCLAuthorizationStatusRestricted||
[CLLocationManager
authorizationStatus]==kCLAuthorizationStatusDenied) {
//用户不允许该程序定位
UIAlertView *tip=[[UIAlertView
alloc]initWithTitle:@"提示"
message:@"用户不允许定位!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[tip show];
}
return
_locationManger;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case
kCLAuthorizationStatusNotDetermined:{
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
}
case
kCLAuthorizationStatusRestricted:{
NSLog(@"kCLAuthorizationStatusRestricted");
break;
}
case
kCLAuthorizationStatusDenied:{
NSLog(@"kCLAuthorizationStatusDenied");
break;
}
case
kCLAuthorizationStatusAuthorizedAlways:{
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
}
case
kCLAuthorizationStatusAuthorizedWhenInUse:{
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
}
default:
NSLog(@"没有这种状态啊");
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"开始更新位置");
NSLog(@"位置数组:%@",locations);
CLLocation *currentLocation=locations[0];
NSString *str=[NSString
stringWithFormat:@"当前的坐标:%f--%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
UIAlertView *tip=[[UIAlertView
alloc]initWithTitle:@"定位"
message:str delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[tip show];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"定位失败:%@",error);
}
@end
以下是一个定位的Demo。
//
// AppleLocationSingle.m
// bk
//
// Created by jie on 16/4/5.
// Copyright © 2016年 jie. All rights reserved.
//
#import "AppleLocationSingle.h"
#import <UIKit/UIKit.h>
@implementation AppleLocationSingle
//定位管理的懒加载
-(CLLocationManager *)locationManger{
if (!_locationManger) {
_locationManger=[[CLLocationManager
alloc]init];
_locationManger.delegate=self;
_locationManger.desiredAccuracy =
kCLLocationAccuracyBest;
/*
当设置为最优的时候,苹果会根据你的精度distanceFilter大小来决定最好的方式满足你的精度。GPS最精确,其次是蜂窝基站三角定位,然后是Wifi通过网络运营商的数据库得到的数据
*/
_locationManger.distanceFilter =
20;//一般是能控制在20米的精度(当系统检测到你的位置没有远离上个位置20米时是不会调取你的代理)
[_locationManger requestAlwaysAuthorization];//处于后台的情况下也可以定位。
[_locationManger
startUpdatingLocation];
}
//判断用户是否允许当前程序使用定位服务
if ([CLLocationManager
authorizationStatus]==kCLAuthorizationStatusNotDetermined || [CLLocationManager
authorizationStatus]==kCLAuthorizationStatusRestricted||
[CLLocationManager
authorizationStatus]==kCLAuthorizationStatusDenied) {
//用户不允许该程序定位
UIAlertView *tip=[[UIAlertView
alloc]initWithTitle:@"提示"
message:@"用户不允许定位!"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[tip show];
}
return
_locationManger;
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case
kCLAuthorizationStatusNotDetermined:{
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
}
case
kCLAuthorizationStatusRestricted:{
NSLog(@"kCLAuthorizationStatusRestricted");
break;
}
case
kCLAuthorizationStatusDenied:{
NSLog(@"kCLAuthorizationStatusDenied");
break;
}
case
kCLAuthorizationStatusAuthorizedAlways:{
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
}
case
kCLAuthorizationStatusAuthorizedWhenInUse:{
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
}
default:
NSLog(@"没有这种状态啊");
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"开始更新位置");
NSLog(@"位置数组:%@",locations);
CLLocation *currentLocation=locations[0];
NSString *str=[NSString
stringWithFormat:@"当前的坐标:%f--%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
UIAlertView *tip=[[UIAlertView
alloc]initWithTitle:@"定位"
message:str delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];
[tip show];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"定位失败:%@",error);
}
@end
相关文章推荐
- iOS 宏(define)与常量(const)的正确使用
- 如何创建你自己的Framework
- iOS地理位置解析
- iOS定位服务 CoreLocation
- 解决在低版本的xcode上使用高版本iOS系统手机进行真机测试
- iOS中二维码,条形码等的扫描
- iOS9 联系人 ContactFramework
- 定位功能iOS SDK
- IOS用CGContextRef画各种图形
- iOS各大名人的博客
- iOS 更新约束
- 谈谈iOS 的加密技术
- iOS开发-NSURLCache(缓存)
- iOS项目开发中 webView 服务器返回来一堆不标准的html 语言 处理办法
- iOS--项目目录组织及生成脚本
- iOS 16进制与10进制的互转
- iOS--苹果设备
- iOS之两个ImageView实现图片滚动
- iOS-Bug收集:CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
- FFmpeg在iOS上完美编译