1.CoreLocation 使用,获取当前位置
1.
ios7只要开始定位,系统就会自动要求你对应用程序授权
ios8之后,必须要代码中实现要求用户对应用程序授权 ,在plist中添加以下两个属性
NSLocationWhenInUseDescription,允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription,允许在后台获取GPS的描述
2,代码
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong)CLLocationManager*manger;
@end
@implementation ViewController
-(CLLocationManager*)manger
{
if (_manger==nil) {
_manger=[[CLLocationManager alloc]init];
}
return _manger;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.manger.delegate=self;
if ([[UIDevice currentDevice].systemVersion floatValue]>8.0) {
[self.manger requestAlwaysAuthorization];//后台运行的时候执行获取位置信息
//[self.manger requestWhenInUseAuthorization];//前台执行的时候获取位置信息
}
else
{
[self.manger startUpdatingLocation];
}
}
//授权状态改变的时候调用
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}else if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
NSLog(@"授权成功");
// 开始定位
[self.manger startUpdatingLocation];
}
else
{
NSLog(@"授权失败");
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//如果只需要获取一次,可以获取位置之后就停止
[self.manger stopUpdatingLocation];
CLLocation *location=[locations lastObject];
NSLog(@"经度=%f,维度=%f 速度=%f ",location.coordinate.latitude,location.coordinate.longitude,location.speed);
}
@end
1.CoreLocation 使用,获取当前位置的更多相关文章
- Uwp Windows10获取设备位置(经纬度)
先在Package.appxmanifest中配置位置权限 2. 创建LocationManager类 using System; using System.Collections.Generic; ...
- js 如何在浏览器中获取当前位置的经纬度
这个有一定的误差哈,具体的误差是多少,有兴趣的朋友可以去测试下 直接上代码 index.html页面代码: <html> <head lang="en"> ...
- jq获取鼠标位置
jq获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- C++获取鼠标位置及全局检测鼠标行为
1.获取鼠标位置(在屏幕的位置) CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ...
- android 获取当前位置
1. Android开发位置感知应用程序方式:1. GPS 定位 精确度高,仅适用于户外,严重消耗电量.如果手机内置GPS接受模块,即使手机处于信号盲区,依然可以获取位置信息. 2. NETW ...
- Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)
通过 JS 或 JQuery 获取到元素后,通过 offsetLeft.offsetTop.offsetWidth.offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参 ...
- jquery 获取鼠标位置
//获取鼠标位置 $(function(){ $('body').mousemove(function(e) { e = e || window.event; __xx = e.pageX || e. ...
- android EditText获取光标位置并安插字符删除字符
android EditText获取光标位置并插入字符删除字符1.获取光标位置int index = editText.getSelectionStart(); 2.在光标处插入字符int index ...
- Android EditText获取光标位置并插入字符删除字符
1.获取光标位置 int index = editText.getSelectionStart(); 2.在光标处插入字符 int index = editText.getSelectionStart ...
随机推荐
- 解决IDEA自动重置LanguageLevel和JavaCompiler版本的问题
使用IDEA时,导入的Maven项目默认的LanguageLevel和JavaCompiler都是1.5,1.5的情况下连最简单的@Override注解都不支持,所以项目可能出现一堆错. 虽然在项目上 ...
- python程序打包成.exe----pyinstaller工具
1. 环境 windows 2. 安装 准备文件:PyWin32 or pypiwin32 运行如下安装命令: pip install pyinstaller==3.0 不要使用3.2版本,编译完成 ...
- C++ 中超类化和子类化常用API
在windows平台上,使用C++实现子类化和超类化常用的API并不多,由于这些API函数的详解和使用方法,网上一大把.本文仅作为笔记,简单的记录一下. 子类化:SetWindowLong,GetWi ...
- C# 静态函数调用窗体控件
回调函数方法是静态函数,需要调用窗体控件,赋值或取值. 定义 public static Form1 mainFrm; mainFrm = this; public partial class F ...
- FFMpeg 滤镜中英文对照
FFMpeg ver 20160213-git-588e2e3 滤镜中英文对照 2016.02.17 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...
- MST:Agri-Net(POJ 1258)
Agri-Net 题目大意:农夫有一片农场,现在他要把这些田地用管子连起来,田地之间有一定距离,铺设每一段管子的长度与这些田地与田地距离是一样的,问你最小的铺设方案. 这一题很裸,Kruskal算法 ...
- storyboard在ios模拟器无法显示的问题
一.问题描述 1.在原有项目新建一个名称为test的storyboard类型的文件. 2.test.storyboard添加View Controller,并设置View Controller下Vie ...
- js正则表达式替换空格
str.replace(/^\s+|\s+$/g, '') 解析: str:要替换的字符串 \s : 表示 space ,空格+: 一个或多个^: 开始,^\s,以空格开始$: 结束,\s$,以空 ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- NIS 报错No such map passwd.byname. Reason: Can't bind to server which serves this domain
在NIS—client端使用命令:ypcat passwd ,把错如上题, 原因:client端ypbind服务未启动解决方法:当然是启动ypbind了,命令:service ypbind start ...