iOS裝置定位

來源:互聯網
上載者:User

iOS裝置定位

一、iOS內建定位

1、SignInSignOutViewController.h

@interface SignInSignOutViewController : UIViewController{    CLLocationManager *_locationManager;    // 緯度    float _latitude;    // 經度    float _longitude;}@property (nonatomic,retain) CLLocationManager *locationManager;@property (nonatomic) float latitude;@property (nonatomic) float longitude;@end

2、SignInSignOutViewController.m

#import "SignInSignOutViewController.h"@interface SignInSignOutViewController ()@end@implementation SignInSignOutViewController@synthesize locationManager = _locationManager;@synthesize latitude = _latitude;@synthesize longitude = _longitude;-(void)dealloc{        self.locationManager = nil;    [super dealloc];}- (void)viewDidUnload{    [super viewDidUnload];    self.locationManager = nil;}- (void)viewDidLoad{    [super viewDidLoad];    // 執行個體化一個位置管理器    CLLocationManager *cllocationManager = [[CLLocationManager alloc] init];    self.locationManager = cllocationManager;    [cllocationManager release];    self.locationManager.delegate = self;    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;    [self.locationManager startUpdatingLocation];    if(![CLLocationManager locationServicesEnabled]){        [GlobalApplication Alert:@"提示":@"請開啟定位:設定 > 隱私 > 位置 > 定位服務"];    }else{        if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {            [GlobalApplication Alert:@"提示":@"定位失敗,請開啟定位:設定 > 隱私 > 位置 > 定位服務 下 XX應用"];        }    } }#pragma mark - CLLocationManagerDelegate#pragma mark - CLLocationManagerDelegate// 地理位置發生改變時觸發- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{    CLLocationCoordinate2D cc,cc2;    // 擷取經緯度    cc.longitude = newLocation.coordinate.longitude;    cc.latitude = newLocation.coordinate.latitude;    // ios座標(google)轉換為 百度座標    cc2 = BMKCoorDictionaryDecode(BMKBaiduCoorForWgs84(cc));    self.longitude = cc2.longitude;    self.latitude = cc2.latitude;    // 停止位置更新    [manager stopUpdatingLocation];}// 定位失誤時觸發- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{    NSString *errorString;    [manager stopUpdatingLocation];    switch([error code]) {        case kCLErrorDenied:            errorString = @"定位失敗,請開啟定位:設定 > 隱私 > 位置 > 定位服務 下 XX應用";            //errorString = @"Access to Location Services denied by user";            break;        case kCLErrorLocationUnknown:            errorString = @"定位失敗,位置資料不可用";            break;        default:            errorString = @"定位失敗,未知錯誤";            break;    }    [GlobalApplication Alert:@"定位":errorString];}

二、百度地圖定位(版本2.1)

1、MainMenuViewController.h

@interface MainMenuViewController : UIViewController {    BMKUserLocation *_mapLocation;    // 緯度    float _latitude;    // 經度    float _longitude;}@property (nonatomic, retain) BMKUserLocation *mapLocation;@property (nonatomic) float latitude;@property (nonatomic) float longitude;@end

2、MainMenuViewController.m

#import "MainMenuViewController.h"@interface MainMenuViewController ()@end@implementation MainMenuViewController@synthesize mapLocation = _mapLocation;@synthesize latitude = _latitude;@synthesize longitude = _longitude; -(void)dealloc{    self.mapLocation = nil;    [super dealloc];    }- (void)viewDidUnload{    [super viewDidUnload];    self.mapLocation = nil;}- (void)viewDidLoad{    [super viewDidLoad];    BMKUserLocation *bmkLocation = [[BMKUserLocation alloc] init];    bmkLocation.delegate = self;    self.mapLocation = bmkLocation;    [bmkLocation release];    [self.mapLocation startUserLocationService];}#pragma mark - baidu map/** *調用startUserLocationService定位成功後,會調用此函數 *@param userLoc 我的位置座標 */- (void)viewDidGetLocatingUser:(CLLocationCoordinate2D)userLoc{    if (userLoc.longitude != 0 && userLoc.latitude != 0 ) {        self.longitude = userLoc.longitude;        self.latitude = userLoc.latitude;        [self.mapLocation stopUserLocationService];    }}

三、結果方法一:
iOS:xxx.151604,xx.170156(iOS採集的座標)
baidu:xxx.162720,xx.174000 (百度轉換的座標)
方法二:
baidu:xxx.162716,xx.173980 (百度採集的座標)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.