iOS8 根據經緯度顯示地名

來源:互聯網
上載者:User

iOS8 根據經緯度顯示地名

   我們很多時候做項目的時候都選用通過手機擷取定位,然後傳資料給後台,這是iOS8根據經緯度顯示地名的demo

  //1.匯入架構

  //

  // NearByViewController.m

  // nearDemo

  //

  // Created by apple on 14/12/5.

  // Copyright (c) 2014年 youdianshang. All rights reserved.

  //

  #import

  #import

  @interface NearByViewController ()

  {

  NSUserDefaults *defaluts;

  NSString * jingduStr;

  NSString *weiduStr;

  NSString *LocationAddress;

  }

  @property(nonatomic,retain)CLLocationManager *locationManager;

  @property(strong,nonatomic)UILabel *positionLabel;

  /*

  經緯度相關設定

  */

  -(void)getLocationAll

  {

  self.locationManager = [[CLLocationManageralloc]init];

  _locationManager.delegate =self;

  _locationManager.desiredAccuracy =kCLLocationAccuracyBest;

  _locationManager.distanceFilter =10;

  [_locationManagerrequestAlwaysAuthorization];//添加這句

  [_locationManagerstartUpdatingLocation];

  }

  /*

  //通過經緯度得到當前地理位置

  -(void)change{

  defaluts=[NSUserDefaultsstandardUserDefaults];

  CLLocationCoordinate2D coordinate;

  coordinate.latitude = [[NSStringstringWithFormat:@"%@",jingduStr]floatValue];

  coordinate.longitude = [[NSStringstringWithFormat:@"%@",weiduStr]floatValue];

  CLLocation *newLocation=[[CLLocationalloc]initWithLatitude:coordinate.latitudelongitude: coordinate.longitude];

  CLGeocoder *geocoder=[[CLGeocoderalloc] init];

  [geocoder reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray* placemarks,NSError* error)

  {

  MKPlacemark*placemark = [placemarks objectAtIndex:0];

  LocationAddress = [NSStringstringWithFormat:@"%@%@%@%@", placemark.locality, placemark.subLocality,placemark.thoroughfare, placemark.subThoroughfare];

  [defaluts setObject:LocationAddress forKey:@"address"];

  NSLog(@"終極目標:1:%@2:%@3:%@4:%@", placemark.locality, placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare);

  }];

  #pragma mark--實現CLLocationManagerDelegate的代理方法,擷取地理位置(鑌)===========================================================================

  //(1)擷取到位置資料,返回的是一個CLLocation的數組,一般使用其中的一個

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

  {

  CLLocation *currLocation = [locations lastObject];

  NSLog(@"經度=%f緯度=%f高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);

  jingduStr =[NSStringstringWithFormat:@"%f", currLocation.coordinate.latitude];

  weiduStr =[NSStringstringWithFormat:@"%f", currLocation.coordinate.longitude];

  NSLog(@"jijijijijij:%@",jingduStr);

  [self change];

  }

  //(2)擷取使用者位置資料失敗的回調方法,在此通知使用者

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

  {

  if ([error code] ==kCLErrorDenied)

  {

  //訪問被拒絕

  }

  if ([errorcode] == kCLErrorLocationUnknown) {

  //無法擷取位置資訊

  }

  }

聯繫我們

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