一,工程圖。 二,代碼。 ViewController.h ViewController.m ...
一,工程圖。
二,代碼。
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