美文网首页
地图解析

地图解析

作者: Wang99 | 来源:发表于2017-12-19 09:18 被阅读0次
#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

相关文章

网友评论

      本文标题:地图解析

      本文链接:https://www.haomeiwen.com/subject/fffbwxtx.html