經由過程CoreLocation定位,獲得到用戶以後地位,跟地圖中的定位分歧。
1、導入CoreLocation.framework
2、#import <CoreLocation/CoreLocation.h>
3、聲明朝理 <CLLocationManagerDelegate>
4、代碼完成
1、聲明
CLLocationManager *locationManager;//界說Manager
// 斷定定位操作能否被許可
if([CLLocationManager locationServicesEnabled]) {
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
}else {
//提醒用戶沒法停止定位操作
}
// 開端定位
[locationManager startUpdatingLocation];
2、更新地位子女理辦法,IOS6.0一下的辦法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
//latitude和lontitude均為NSString型變量
//緯度
self.latitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.latitude];
//經度
self.longitude = [NSString stringWithFormat:@"%.4f", newLocation.coordinate.longitude];
}
3、IOS6.0以上蘋果的推舉辦法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//此處locations存儲了連續更新的地位坐標值,取最初一個值為最新地位,假如不想讓其連續更新地位,則在此辦法中獲得到一個值以後讓locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
CLLocationCoordinate2D coor = currentLocation.coordinate;
self.latitude = coor.latitude;
self.longitude = coor.longitude;
//[self.locationManager stopUpdatingLocation];
}
4、更新掉敗的辦法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
// 提醒用戶失足緣由,可按住Option鍵點擊 KCLErrorDenied的檢查更多失足信息,可打印error.code值查找緣由地點
}
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐本站。
【iOS獲得到用戶以後地位】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!