关于定位我也是通过学习ios5 pragram cookbook中的第六章学习才得以解决但是在第六章中使用的地理位置反编码技术只能在ios5系统中才能实现在低版本的ios是不能兼容的所以在里我使用了两种方法来实现定位。
其定位有3种方式:
1,GPS,最精确的定位方式
2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。
3,Wifi,这种方式貌似是通过网络运营商的数据库得到的数据,在3种定位种最不精确
首先你要在你的Xcode中添加两个连接库,MapKit和CoreLocation,如图
core location提供了定位功能,能定位装置的当前坐标,同时能得到装置移动信息,最重要的类是CLLocationManager,定位管理。
首相我们先定位,在手机上得到手机在地球上所处的经度和维度通过经度下面以一段是定位的启动程序
if([CLLocationManager locationServicesEnabled]) { self.myLocationManager = [[CLLocationManager alloc]init]; self.myLocationManager.delegate = self; self.myLocationManager.purpose = @"To provide functionality based on user's current location."; // NSLog(@"PURPOSE = %@",self.myLocationManager.purpose); //选择定位的方式为最优的状态,他又四种方式在文档中能查到
self.myLocationManager.desiredAccuracy=kCLLocationAccuracyBest; //发生事件的最小距离间隔
self.myLocationManager.distanceFilter = 1000.0f; [self.myLocationManager startUpdatingLocation]; }
在iOS4中的解决方案
很多时候我们需要把它反向编码成普通人能读懂的地理位置描述如:X国XX市XXX区XXX街道XX号,这就需要用到MapKit中的一个地理位置反向编码工具:MKReverseGeocoder,首相我们要利用CLLocationManagerDelegate,因为将坐标信息发到服务器再反回来需要一定的时间,所以为了防止阻塞,发出信息后并不知到什么时候会返回信息,信息返回时会通知委托方法。这里实现这个类主要时为了实现2个方法如下
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { } -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"reverse geocoder error: %@", [error description]); }
didFailWithError这个方法是来处理返回错误信息的,didFindPlacemark则是地理信息返回了,地理信息包含在placemark里面,此对象中包含国家,城市,区块,街道等成员变量。
然后初始化一个MKReverseGeocoder(地理反向编码)
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc]initWithCoordinate:self.myLocation.coordinate]; geocoder.delegate = self; //启动gecoder
[geocoder start];
而在ios5中的解决方案
在IOS5中不用MKReverseGeocoder反向编码,而是使用CLLocationManagerDelegate直接根据经度和维度反向编码生成地理位置,代码在下面的注释部分。
需要注意的是[self.myLocationManager startUpdatingLocation];这里的更新可能会在的你终端中输出好几次,所以在得到地址之后应该及时[self.myLocationManager stopUpdatingLocation];
#import "ViewController.h"
@interface ViewController () @end
@implementation ViewController @synthesize myLocationManager; @synthesize myGecoder; @synthesize myLocation; #pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { static int i = 0; i++; NSLog(@"I = %d", i); NSLog(@"Latitude = %f", newLocation.coordinate.latitude); NSLog(@"Longitude = %f", newLocation.coordinate.longitude); self.myLocation = newLocation; // self.myGecoder = [[CLGeocoder alloc] init]; // [self.myGecoder reverseGeocodeLocation:self.myLocation completionHandler:^(NSArray *placemarks, NSError *error) // { // if(error == nil && [placemarks count]>0) // { // CLPlacemark *placemark = [placemarks objectAtIndex:0]; //
// NSLog(@"Country = %@", placemark.country); // NSLog(@"Postal Code = %@", placemark.postalCode); // NSLog(@"Locality = %@", placemark.locality); // NSLog(@"address = %@",placemark.name); // NSLog(@"administrativeArea = %@",placemark.administrativeArea); // NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea); // NSLog(@"locality = %@", placemark.locality); // NSLog(@"thoroughfare = %@", placemark.thoroughfare); // } // else if(error==nil && [placemarks count]==0){ // NSLog(@"No results were returned."); // } // else if(error != nil) { // NSLog(@"An error occurred = %@", error); // } // }]; // [self.myLocationManager stopUpdatingLocation];
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc]initWithCoordinate:self.myLocation.coordinate]; geocoder.delegate = self; [geocoder start]; } -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { } -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"reverse geocoder error: %@", [error description]); } -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { NSLog(@"Country = %@", placemark.country); NSLog(@"Postal Code = %@", placemark.postalCode); NSLog(@"Locality = %@", placemark.locality); NSLog(@"address = %@",placemark.name); NSLog(@"administrativeArea = %@",placemark.administrativeArea); NSLog(@"subAdministrativeArea = %@",placemark.subAdministrativeArea); NSLog(@"locality = %@", placemark.locality); NSLog(@"thoroughfare = %@", placemark.thoroughfare); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.
if([CLLocationManager locationServicesEnabled]) { self.myLocationManager = [[CLLocationManager alloc]init]; self.myLocationManager.delegate = self; self.myLocationManager.purpose = @"To provide functionality based on user's current location."; // NSLog(@"PURPOSE = %@",self.myLocationManager.purpose);
self.myLocationManager.desiredAccuracy=kCLLocationAccuracyBest; self.myLocationManager.distanceFilter = 1000.0f; [self.myLocationManager startUpdatingLocation]; } else { NSLog(@"Location services are not enabled"); } } - (void)viewDidUnload { [super viewDidUnload]; self.myLocationManager = nil; self.myGecoder = nil; // Release any retained subviews of the main view.
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(void)dealloc { [self.myLocation release]; [self.myLocationManager release]; [self.myGecoder release]; [super dealloc]; } @end