iOS Core Location 实现定位
在iphone中可以用core location功能来实现地理定位,并可用mapkit 框架加载google地图。
想得到定点的信息,其实 不难,只需要涉及到几个类,CLLocationManager, CLLocation, CLLocationManagerdelegate协 议,CLLocationCoodinate2D, CLLocationDegrees。
<一>先实例化一个CLLocationManager,同时设置委托及精确度等。
CCLocationManager *manager = [[CLLocationManager alloc] init];
[manager setDelegate: self];
[manager setDesiredAccuracy: kCLLocationAccuracyBest];
其中desiredAccuracy属性表示精确度,有利5种选择如下:desiredAccuracy属性描述
kCLLocationAccuracyBest
精确度最佳
kCLLocationAccuracynearestTenMeters
精确度10m以内
kCLLocationAccuracyHundredMeters
精确度100m以内
kCLLocationAccuracyKilometer
精确度1000m以内
kCLLocationAccuracyThreeKilometers
精确度3000m以内
NOTE:精确度越高,用点越多,就要根据实际情况而定。
manager.distanceFilter = 250;这个表示在地图上每隔250m才更新一次定位信息。
[manager startUpdateLocation]; 启动定位器,如果不用的时候就必须调用stopUpdateLocation以关闭定位功能。
<二>CCLocation对像中包含着定点的相关信息数据。其属性主要包括coordinate, altitude,horizontalAccuracy,verticalAccuracy, timestamp等,分别如下:
coordinate 用来存储地理位置的latitude和longitude,分别表示 纬度和经度,都是float类型.如可这 样: float latitude = location.coordinat.latitude; location是CCLocation的实例。 这里也把上面提到的CLLocationDegrees,它其实是一个double类型,在core Location框架中是用来储存 CLLocationCoordinate2D实例coordinate的latitude 和longitude,
typedef double CLLocationDegrees;
typedef struct {CLLocationDegrees latitude; CLLocationDegrees longitude} CLLocationCoordinate2D;
altitude 表示位置的海拔高度,这个值是极不准确的。
horizontalAccuracy 表示水平准确度,这么理解,它是以coordinate为圆心的半径,返回的值越小,证明准确度越好,如果是负数,则表示core location定位失败。
verticalAccuracy表示垂直准确度,它的返回值与altitude相关,所以不准确。
Timestamp 返回的是定位时的时间,是NSDate类型。
<三>CLLocationMangerDelegate协议我们只需实现两个方法就可以了,如下:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation ;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
上面第一个是定位时候回访调,后者定位出错时被调。
<四>现在可以去实现定位了:
新建一个view-based application模板的工程,假设项目名称为coreLocation.我们在contronller的头文件和源文件中的代码大概有如下:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CoreLocationViewController : UIViewController
<CLLocationManagerDelegate>{CLLocationManager *locManager;
}
@property (nonatomic, retain) CLLocationManager *locManager;
@end
.m
#import "CoreLocationViewController.h"
@implementation CoreLocationViewController
@synthesize locManager;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBest;
[locManager startUpdatingLocation];[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[locManager stopUpdatingLocation];
[locManager release];
[textView release];[super dealloc];
}
#pragma mark -
#pragma mark CoreLocation Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D locat = [newLocation coordinate];
float lattitude = locat.latitude;
float longitude = locat.longitude;
float horizon = newLocation.horizontalAccuracy;
float vertical = newLocation.verticalAccuracy;
NSString *strShow = [[NSString alloc] initWithFormat:
@"currentpos: 经度=%f 维度=%f 水平准确读=%f 垂直准确度=%f ",
lattitude, longitude, horizon, vertical];
UIAlertView *show = [[UIAlertView alloc] initWithTitle:@"coreLoacation" message:strShow delegate:nil cancelButtonTitle:@"i got it"otherButtonTitles:nil];
[show show];
[show release];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSString *errorMessage;
if ([error code] == kCLErrorDenied){errorMessage = @"你的访问被拒绝";}
if ([error code] == kCLErrorLocationUnknown) {errorMessage = @"无法定位到你的位置!";}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:errorMessage delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
iOS Core Location 实现定位相关推荐
- Core Location :⽤用于地理定位
Core Location :⽤用于地理定位 在移动互联⽹网时代,移动app能解决⽤用户的很多⽣生活琐事,⽐比如 导航:去任意陌⽣生的地⽅方 周边:找餐馆.找酒店.找银⾏行.找电影院 在上述应⽤用中, ...
- 《iPhone开发基础教程》第13章 我在哪里?使用Core Location定位功能
Core Location可以利用三种技术实现定位:GPS.蜂窝基站三角网定位(cell tower triangulation)和Wi-Fi定位服务(WPS). GPS是最精确的,但在第一代iPho ...
- 简述Core Location定位功能
移动设备的定位功能很强大,可以做很多有意思的应用,比如LBS,GPS等等. Core Location依赖于3种不同的方式,每种方式在给定的平台上可能可用,也可能不可用.这些方式受每种设备内置功能的限 ...
- Core Location定位
Core Location主要应用了GPS, 蜂窝基站三角网以及Wi_Fi(WPS)三种技术. 使用GPS定位系统,可以精确地定位你当前所在的地理位置,但由于GPS接收机需要对准天空才能工作,因此在室 ...
- iOS开发--地图与定位
iOS开发--地图与定位 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们 ...
- Core Location和MapKit的一些简单使用
Core Location 1. 基本对象是CLLocation,有属性coordinate, altitude, horizontal/vertical Accuracy, timestamp, s ...
- iOS Swift 使用 CLLocationManager 定位
iOS Swift 使用 CLLocationManager 定位 CLLocationManager 是IOS 系统提供的定位对象,通过该对象可以获取定位信息,包括:经纬度.海拔.方向.速度.通过反 ...
- 图层几何学 -- iOS Core Animation 系列二
<图层树和寄宿图 -- iOS Core Animation 系列一>介绍了图层的基础知识和一些属性方法.这篇主要内容是学习下图层在父图层上怎么控制位置和尺寸的. 1.布局 首先看一张例图 ...
- IOS Core Image之二
在上篇博客IOS Core Image之一中了解了下CIImage.CIFilter.CIContext三个类的使用,这篇了解下滤镜链(多滤镜)和人脸检测(不是人脸识别). 一.多滤镜 1.有些效果不 ...
最新文章
- matrix_multiply代码解析
- 【spring】在不联网的情况下查看xml的定义规则的方法
- 蜜糖变砒霜:90%美国公司区块链项目将不再重启
- 前端跨域请求get_HTTP--跨域真的有这么难吗
- 第一期站立会议2(11.16个人)
- stm32非操作系统开发和带uCos的开发的区别,及一些解析
- HTML、JSP、Servlet中的相对路径和绝对路径 页面跳转问题
- GDCM:gdcm::Version的测试程序
- PHP中$_SERVER的详细参数
- MATLAB中的randi函数
- VC的文件路径为什么要用双斜杠
- adb logcat 基本用法
- ajax jsonp post php,※ajax的post方式注意点※及jsonp的封装
- SELECTION-SCREEN 加按钮
- 【广工考试笔记】计算机系统结构考试速成笔记
- 知乎:1.03亿月活用户,获得感战略下的内容质量和商业化布局难平衡
- 台式计算机关机后自行重启,台式电脑关机后自动重启怎么办?台式电脑关机后自动开机的处理办法...
- hive分隔符_Hive中默认分隔符介绍
- 华为的芯片战略:别忘记,代号—SD502!
- 企业微信聊天框中点击自建应用获取当前聊天外部联系人userid,wx.agentConfig调用