使用系统的CoreLocation定位
// ViewController.m
// LBS
//
// Created by tonnyhuang on 15/8/28.
// Copyright (c) 2015年 tonnyhuang. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
//首先,我们需要在工程中导入CoreLocation系统框架。然后在我们的控制器中引入头文件。
//然后,声明一个CLLocationManager对象作为成员变量,用于定位获取经纬度坐标,并遵守协议CLLocationManager的协议。
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *_locationManager;
}
@end
@implementation ViewController
//实现其中的代理方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//获取经度
NSLog(@"经度 == %lf", newLocation.coordinate.longitude);
//获取纬度
NSLog(@"纬度 == %lf", newLocation.coordinate.latitude);
//获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编码出地址信息
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"%@", placemark.name);
//获取城市
NSString *city = placemark.locality;
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市
if (!city) {
city = placemark.administrativeArea;
} else if (error == nil && [placemarks count] == 0){
NSLog(@"no result were returned");
} else if (error != nil) {
NSLog(@"error = %@", error);
}
NSLog(@"city = %@", city);
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
}
//最后在viewDidLoad中初始化定位管理器。
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeLocationService];
}
- (void)initializeLocationService {
//初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
//设置代理
_locationManager.delegate = self;
//设置定位精确度到米
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置过滤器为无
_locationManager.distanceFilter = kCLDistanceFilterNone;
//开始定位
[_locationManager requestAlwaysAuthorization];
//取得定位权限,有两个方法,取决于你的定位使用情况
[_locationManager startUpdatingLocation];
}
//如果需要正常定位,相对iOS7而言,iOS8需要额外处理两个地方。//1. 工程的plist文件里面添加两个字段:NSLocationAlwaysUsageDescription,
// 开始定位
// 取得定位权限,有两个方法,取决于你的定位使用情况
// 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
//[_locationManager requestAlwaysAuthorization];//这句话ios8以上版本使用。
//[_locationManager startUpdatingLocation];
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
使用系统的CoreLocation定位的更多相关文章
- iOS - CoreLocation 定位
前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...
- iOS开发拓展篇—CoreLocation定位服务
iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...
- iOS-----使用CoreLocation定位
使用CoreLocation定位 CoreLocation框架 (CoreLocation.framework)可用于定位设备当前经纬度, 通过该框架, 应用程序可通过附近的蜂窝基站\WIFI信号 或 ...
- iOS8中使用CoreLocation定位[转]
本文转自:http://blog.devzeng.com/blog/ios8-corelocation-framework.html iOS8以前使用CoreLocation定位 1.首先定义一个全局 ...
- CoreLocation定位技术
CoreLocation框架可用于定位设备当前经纬度,通过该框架,应用程序可通过附近的蜂窝基站,WIFI信号或者GPS等信息计算用户位置. iOS定位支持的3种模式. (1)GPS ...
- CoreLocation 定位
前言: 本章会使用OC和Swift分别进行实现,需要了解Swift的小伙伴可以翻一下之前的博文 LBS和SoloMo(索罗门) LBS:基于位置的服务,根据定位展示周边美食.景点等信息(全称:Loca ...
- CoreLocation定位
nCoreLocation n简介 n在移动互联网时代,移动app能解决用户的很多生活琐事,比如 p导航:去任意陌生的地方 p周边:找餐馆.找酒店.找银行.找电影院 p n在上述应用中,都用到了地 ...
- iOS iOS9.0 的CoreLocation定位
一.简介 iOS9.0如果当前处于前台授权状态,默认是不可以后台获取用户位置. 如果在前台授权下,让其能获取到后台定位,该怎么办 可以设置以下属性为YES,就可以继续获取后台位置,但是会出现蓝条 使用 ...
- Android 系统api实现定位及使用百度提供的api来实现定位
目前在国内使用定位的方法主要是 1. Android系统提供的 LocationManager locationManager = (LocationManager) getSystemService ...
随机推荐
- Java并发知识(2)
1. 什么是原子操作?在Java Concurrency API中有哪些原子类(atomic classes)? 原子操作是指一个不受其他操作影响的操作任务单元.原子操作是在多线程环境下避免数据不一致 ...
- CSS选择器学习小结
关于CSS选择器的问题,在实际项目中,以及一般的前端面试中会经常遇到.下面对此做一小结,梳理和巩固相关方面知识.(如有不妥之处,还望大家及时批评指正,以免误导他人) 一.选择器种类 1.id选择器(# ...
- UGUI双击事件
经测试在Android.ios平台下无效 using UnityEngine; using UnityEngine.EventSystems; using System.Collections; us ...
- 批量导入数据(Mysql)报MySQL server has gone away 问题的解决方法
问题分析 首先度娘:mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造 ...
- 01Tensorflow学习之Tensorflow基本介绍
1 tensorflow简介 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着基 ...
- golang interface接口
package main import "fmt" type Shaper interface { Area() float32 } type Square struct { si ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 重哈希 · rehashing
[抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: newindex = (hashTable[ ...
- mysql简单实现查询结果添加序列号的方法
方法1: SELECT @rownum :=@rownum + 1 AS rownum, t.* FROM integral_system_user t, (SELECT @rownum := 0) ...
- Laravel框架中实现supervisor执行异步进程
问题描述:在使用Laravel框架实现动态网页时,若有些操作计算量较大,为了不影响用户体验,往往需要使用异步方式去处理.这里使用supervisor和laravel自带的queues实现. Super ...