歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS通過經緯度反向解析地址

iOS通過經緯度反向解析地址

日期:2017/3/1 10:55:34   编辑:Linux編程

1. 在工程裡面引入CoreLocation.framework和MapKit.framework。

2. 在.h文件裡面加入如下代碼:

  1. #import <CoreLocation/CoreLocation.h>
  2. #import <MapKit/MKReverseGeocoder.h>
  3. #import <MapKit/MKPlacemark.h>
  1. @interface RootViewController : UICustomViewController<CLLocationManagerDelegate, MKReverseGeocoderDelegate> {
  2. CLLocationManager *gps;
  3. }

3. 在.m文件中加入以下代碼:

  1. - (void)locationManager:(CLLocationManager *)locationManager didUpdateToLocation:(CLLocation *)newLocation
  2. fromLocation:(CLLocation *) oldLocation;
  3. {
  4. self.location = [NSString stringWithFormat:@"%f,%f",newLocation.coordinate.latitude, newLocation.coordinate.longitude];
  5. [self startedReverseGeoderWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
  6. }
  7. - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
  8. if ( [error code] == kCLErrorDenied ) {
  9. [manager stopUpdatingHeading];
  10. } else if ([error code] == kCLErrorHeadingFailure) {
  11. }
  12. }
  13. -(void) startedReverseGeoderWithLatitude:(double)latitude longitude:(double)longitude{
  14. CLLocationCoordinate2D coordinate2D;
  15. coordinate2D.longitude = longitude;
  16. coordinate2D.latitude = latitude;
  17. MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate2D];
  18. geoCoder.delegate = self;
  19. [geoCoder start];
  20. }
  21. -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
  22. {
  23. NSString *subthroung=placemark.thoroughfare;
  24. NSString *local=placemark.locality;
  25. self.textFieldName.text = [NSString stringWithFormat:@"您當前所在位置:%@,%@",local, subthroung];
  26. }
  27. -(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
  28. {
  29. }

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. gps = [[CLLocationManager alloc] init];
  4. gps.delegate = self;
  5. gps.desiredAccuracy = kCLLocationAccuracyBest;
  6. gps.distanceFilter = kCLDistanceFilterNone;
  7. [gps startUpdatingLocation];
  8. }

編譯運行下就能看到結果了,哈哈~

Copyright © Linux教程網 All Rights Reserved