iOS開發-定位服務iOS8.0以上授權

來源:互聯網
上載者:User

標籤:

if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0) {

        [self.locMgr requestAlwaysAuthorization];

    } else {

        [self.locMgr startUpdatingLocation];

    }

 

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    if (status == kCLAuthorizationStatusNotDetermined) {

        NSLog(@"等待使用者授權");

    }

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {

        // 得到使用者授權開始定位

        [self.locMgr startUpdatingLocation];

        

    } else {

        NSLog(@"授權失敗");

    }

}

/**

 *  當定位到使用者的位置時,就會調用(調用的頻率比較頻繁)

 */

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

 

    // locations 數組裡存放的是 CLLocation 對象, 一個 CLLocation 對象就代表著一個位置

    CLLocation *loc = [locations firstObject];

    // 緯度 : loc.coordinate.latitude

    // 經度 : loc.coordinate.longitude

    NSLog(@"緯度:%f, 經度:%f",loc.coordinate.latitude,loc.coordinate.longitude);

    

    

    CLLocation *c = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];

    //建立位置

    CLGeocoder *revGeo = [[CLGeocoder alloc] init];

    [revGeo reverseGeocodeLocation:c completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        if (!error && [placemarks count] > 0) {

            NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];

            NSLog(@"%@",[dict objectForKey:@"Country"]);

            NSLog(@"%@",[dict objectForKey:@"State"]);

            NSLog(@"%@",[dict objectForKey:@"SubLocality"]);

            NSLog(@"%@",[dict objectForKey:@"Street"]);//  Country(國家)  State(城市)  SubLocality(區) Street(詳細)

        } else {

            NSLog(@"ERROR: %@", error); }

    }];

 

    [self.locMgr stopUpdatingLocation];

}

 

iOS開發-定位服務iOS8.0以上授權

聯繫我們

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