ios定位服務

來源:互聯網
上載者:User

標籤:

ios不能指定採用哪種定位方式,會根據裝置的情況和周圍的環境採用一套最佳的解決方案。

再定位服務的應用中,第一次請求位置資訊時,系統會提示使用者是否允許開啟定位服務。

Ios主要通過三個類來實現定位:

1)        CLLocationManager,用於定位服務管理類,它能夠給我們提供位置資訊和高度資訊。

2)        CLLocationManagerDelegate,它是CLLocationManager類的委託協議

3)        CLLocation.該類封裝了位置和高度資訊

CLLocationManager類的desiredAccurecy屬性,有6個取值。

            kCLLocationAccuracyNearestTenMeters,精確到10米

            kCLLocatinAccuracyHundredMeters,精確懂100米

            kCLLocatinAccuracyKilometer,精確到1000米

            kCLLocatinAccuracyThreeKilometer,精確到3000米

            kCLLocatinAccuracyBest,裝置使用電池供電時最高精度

            kCLLocatinAccuracyBestForNavigation,導航情況下的最高精度

 兩個代理方法:

locationManager:didUpdateLocations:定位成功

locationManager:didFailWithError:定位失敗

 

代碼如下:

#import "LocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>

@interface LocationViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager * locationManager;

@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //初始化locationManager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 0.1f;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //開始定位
    [self.locationManager startUpdatingLocation];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //停止定位
    [self.locationManager stopUpdatingLocation];
}

#pragma mark - 代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation * location = [locations lastObject];
    NSLog(@"%3.5f", location.coordinate.latitude);//經度
    NSLog(@"%3.5f", location.coordinate.longitude);//緯度
    NSLog(@"%3.5f", location.altitude);//高度
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"error: %@", error);
}

@end

ios定位服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.