#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *addressTF;
@property (weak, nonatomic) IBOutlet UITextField *jingDu;
@property (weak, nonatomic) IBOutlet UITextField *weiDu;
@property (weak, nonatomic) IBOutlet UITextView *myTextView;
@property (nonatomic,strong) CLGeocoder *geocoder;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 創建地址解析器
self.geocoder = [[CLGeocoder alloc] init];
}
- (IBAction)openParsingButton:(id)sender {
// 獲取用戶輸入的地址字符串
NSString* addr = self.addressTF.text;
if(addr != nil && addr.length > 0)
{
[self.geocoder geocodeAddressString:addr completionHandler: ^(NSArray *placemarks, NSError *error)
{
// 如果解析結果的集合元素的個數大于1,表明解析得到了經度、緯度信息
if (placemarks.count > 0)
{
// 只處理第一個解析結果,實際項目中可使用列表讓用戶選擇
CLPlacemark* placemark = placemarks[0];
CLLocation* location = placemark.location;
self.myTextView.text = [NSString stringWithFormat:@"%@的緯度為:%g,經度為:%g" , addr ,location.coordinate.latitude,location.coordinate.longitude];
}
// 沒有得到解析結果。
else
{
// 使用UIAlertView提醒用戶
[[[UIAlertView alloc] initWithTitle:@"提醒" message:@"您輸入的地址無法解析" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]show];
}
}];
}
}
- (IBAction)theParsingButton:(id)sender {
//經度
NSString* longitudeStr = self.weiDu.text;
//緯度
NSString* latitudeStr = self.jingDu.text;
if(longitudeStr != nil && longitudeStr.length > 0&& latitudeStr != nil && latitudeStr.length > 0)
{
// 將用戶輸入的經度、緯度封裝成CLLocation對象
CLLocation* location = [[CLLocation alloc]initWithLatitude:[latitudeStr floatValue]longitude:[longitudeStr floatValue]];
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
// 如果解析結果的集合元素的個數大于1,表明解析得到了經度、緯度信息
if (placemarks.count > 0)
{
// 只處理第一個解析結果,實際項目可使用列表讓用戶選擇
CLPlacemark* placemark = placemarks[0];
// 獲取詳細地址信息
NSArray* addrArray = [placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
// 將詳細地址拼接成一個字符串
NSMutableString* addr = [[NSMutableString alloc] init];
for(int i = 0 ; i < addrArray.count ; i ++)
{
[addr appendString:addrArray[i]];
}
self.myTextView.text = [NSString stringWithFormat: @"經度:%g,緯度:%g的地址為:%@" ,location.coordinate.longitude ,location.coordinate.latitude , addr];
}
// 沒有得到解析結果。
else
{
// 使用UIAlertView提醒用戶
[[[UIAlertView alloc] initWithTitle:@"提醒" message:@"您輸入的地址無法解析" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]
show];
}
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
地圖解析
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。