關於定位我也是通過學習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
