GPS定位

来源:互联网 发布:qt高级编程丁林松 ppt 编辑:程序博客网 时间:2024/05/01 03:45

转载:http://www.cnblogs.com/ylucy/archive/2011/08/30/2159924.html

Core Location主要应用了GPS, 蜂窝基站三角网以及Wi_Fi(WPS)三种技术。

  • 使用GPS定位系统,可以精确地定位你当前所在的地理位置,但由于GPS接收机需要对准天空才能工作,因此在室内环境基本无用。
  • 另一个找到自己所在位置的有效方法是使用手机基站,手机开机时,它会与周围的基站保持联系,如果你知道这些基站的身份,就可以使用各种数据库(包含基站的身份和它们的确切地理位置)计算出手机的物理位置。基站不需要卫星,和GPS不同,它对室内环境一样管用。但它没有GPS那样精确,它的精度取决于基站的密度,它在基站密集型区域的准确度最高。
  • 第三种方法是依赖Wi-Fi,使用这种方法时,设备连接到Wi-Fi网络,通过检查服务提供商的数据确定位置,它既不依赖卫星,也不依赖基站,因此这个方法对于可以连接到Wi-Fi网络的区域有效,但它的精确度也是这三个方法中最差的。

想得到定点的信息,需要涉及到几个类,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:nilmessage:errorMessage

      delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];

[alert show];

[alert release];

}

@end

原创粉丝点击