iOS将地址解析成经纬度的方法

来自:互联网
时间:2020-05-26
阅读:
免费资源网 - https://freexyz.cn/

本文实例为大家分享了iOS将地址解析成经纬度的具体代码,供大家参考,具体内容如下

一、工程图

iOS将地址解析成经纬度的方法

二、代码

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>

@interface ViewController : UIViewController
<CLLocationManagerDelegate>

@end

ViewController.m

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>


//正编译。将地址解析为经纬度
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  
  
  CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  [geocoder geocodeAddressString:@"上海徐汇漕溪路" completionHandler:^(NSArray *placemarks, NSError *error){
    NSLog(@"查询记录数:%ld",[placemarks count]);
    if ([placemarks count] > 0) {
      CLPlacemark *placemark = [placemarks objectAtIndex:0];
      CLLocationCoordinate2D coordinate = placemark.location.coordinate;
      
      NSString *strCoordinate = [NSString stringWithFormat:@"经度:%3.5f 纬度:%3.5f:",coordinate.latitude,coordinate.longitude ];
      NSLog(@"%@",strCoordinate);
    }
  }];

}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

免费资源网 - https://freexyz.cn/
返回顶部
顶部