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 使用,获取当前位置的更多相关文章

  1. Uwp Windows10获取设备位置(经纬度)

    先在Package.appxmanifest中配置位置权限 2. 创建LocationManager类 using System; using System.Collections.Generic; ...

  2. js 如何在浏览器中获取当前位置的经纬度

    这个有一定的误差哈,具体的误差是多少,有兴趣的朋友可以去测试下 直接上代码 index.html页面代码: <html> <head lang="en"> ...

  3. jq获取鼠标位置

    jq获取鼠标位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. C++获取鼠标位置及全局检测鼠标行为

    1.获取鼠标位置(在屏幕的位置)  CPoint m_mouse; GetCursorPos(&m_mouse); 2. 屏幕转化为客户端(控件的相对位置)& 客户端位置转化为屏幕位置 ...

  5. android 获取当前位置

    1. Android开发位置感知应用程序方式:1. GPS 定位     精确度高,仅适用于户外,严重消耗电量.如果手机内置GPS接受模块,即使手机处于信号盲区,依然可以获取位置信息. 2. NETW ...

  6. Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)

    通过 JS 或 JQuery 获取到元素后,通过 offsetLeft.offsetTop.offsetWidth.offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参 ...

  7. jquery 获取鼠标位置

    //获取鼠标位置 $(function(){ $('body').mousemove(function(e) { e = e || window.event; __xx = e.pageX || e. ...

  8. android EditText获取光标位置并安插字符删除字符

    android EditText获取光标位置并插入字符删除字符1.获取光标位置int index = editText.getSelectionStart(); 2.在光标处插入字符int index ...

  9. Android EditText获取光标位置并插入字符删除字符

    1.获取光标位置 int index = editText.getSelectionStart(); 2.在光标处插入字符 int index = editText.getSelectionStart ...

随机推荐

  1. Unity3d《Shader篇》法线贴图

    效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...

  2. 15.SpringMVC和Spring上下文关系(为什么SpringMVC可以调用到Spring)

    springmvc上下文继承于spring, 也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean. spring上下文是spring启 ...

  3. WdatePicker组件不显示

    突然发现时间组件不显示了,以为是浏览器的问题.在本地服务器测试了一下发现一切正常. 怀疑是前段时间中毒引起的,用工具比对了一下WdatePicker的文件包,发现My97DatePicker.htm这 ...

  4. iOS SHA1加密实现方法

    使用方法 先导入头文件 #import "SHA1.h" //SHA1测试 NSString* sh1=[SHA1 getSha1String:"]; NSLog(@&q ...

  5. bootstrap-datepicker的使用

    转载自:http://michael-roshen.iteye.com/blog/1779541 在普通的网页中显示datepicker比较简单,将bootstrap-datepicker-zh_CN ...

  6. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. NEFU 504 new Flip Game (高斯消元)

    题目链接 题解:和 poj1753Filp game 差不多,区别在于t组数据并且翻转的时候多了一个左上角. #include <iostream> #include <cstdio ...

  8. css3属性flex弹性布局设置三列(四列)分布样式

    参考:阮一峰的网络日志 <!doctype html> <html lang="en"> <head> <meta charset=&qu ...

  9. oracle学习不错的网站

    http://oracle-base.com/articles/linux/rlwrap.php

  10. Maven 在sts不会自动下载包的问题

    1.查看maven配置setting.xml是否有设置远程仓库 2.sts是否正确配置指定了setting.xml 3.是否开启线上下载,如下图