亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取用戶當前的城市名稱?

如何獲取用戶當前的城市名稱?

阿波羅的戰車 2019-11-05 16:16:37
您如何檢索用戶的當前城市名稱?
查看完整描述

3 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您需要做的是設置一個CLLocationManager可以找到您當前坐標的坐標。使用當前坐標,您需要使用它MKReverseGeoCoder來找到您的位置。


- (void)viewDidLoad 

{  

    // this creates the CCLocationManager that will find your current location

    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];

    locationManager.delegate = self;

    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    [locationManager startUpdatingLocation];

}


// this delegate is called when the app successfully finds your current location

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 

{

    // this creates a MKReverseGeocoder to find a placemark using the found coordinates

    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];

    geoCoder.delegate = self;

    [geoCoder start];

}


// this delegate method is called if an error occurs in locating your current location

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 

{

 NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);

}


// this delegate is called when the reverseGeocoder finds a placemark

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark

{

    MKPlacemark * myPlacemark = placemark;

    // with the placemark you can now retrieve the city name

    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];

}


// this delegate is called when the reversegeocoder fails to find a placemark

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error

{

    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);

}


查看完整回答
反對 回復 2019-11-05
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

從iOS 5開始MKReverseGeoCoder不推薦使用!


因此,您想使用CLGeocoderwith CLLocationManager,非常簡單,并且可以使用block。


例:


- (void)locationManager:(CLLocationManager *)manager

    didUpdateToLocation:(CLLocation *)newLocation

           fromLocation:(CLLocation *)oldLocation

{

   [self.locationManager stopUpdatingLocation];


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

    [geoCoder reverseGeocodeLocation:newLocation

                   completionHandler:^(NSArray *placemarks, NSError *error) {

                       for (CLPlacemark *placemark in placemarks) {

                           .... = [placemark locality];

                       }

                   }];

}

編輯:除了for in循環,您還可以執行以下操作:


NSString *locString = placemarks.count ? [placemarks.firstObject locality] : @"Not Found";


查看完整回答
反對 回復 2019-11-05
  • 3 回答
  • 0 關注
  • 952 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號