iOS9下有关CoreLocation学习三

来源:互联网 发布:西安软件开发 大专 编辑:程序博客网 时间:2024/05/29 12:38

CoreLocation


地理编码

CLGeocoder:地理编码器,其中Geo是地理的英文单词Geography的简写。

  • 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
  • 反地理编码:根据给定的经纬度,获得具体的位置信息

地理编码方法

  - (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler; 

反地理编码方法

  - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;

学习代码如下:

////  ViewController.m//  CoreLocationTwo////  Created by LoveQiuYi on 16/2/23.//  Copyright © 2016年 LoveQiuYi. All rights reserved.//  地理编码#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *locationName;@property (weak, nonatomic) IBOutlet UILabel *addressLabel;//经度@property (weak, nonatomic) IBOutlet UITextField *longtitudeField;//纬度@property (weak, nonatomic) IBOutlet UITextField *altitudeField;@property (nonatomic, strong) CLGeocoder * geocoder;@end@implementation ViewController- (IBAction)click {    //获取文本框中的输入信息    NSString * stringName = self.locationName.text;    if (stringName.length == 0) {        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入地名" preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"");        }];        [alertController addAction:alertAction];        //不调用这个方法会报错        [self presentViewController:alertController animated:YES completion:nil];    }    //-------------地理编码:地址转为经纬度-----------    [self.geocoder geocodeAddressString:stringName completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {        //判断这个地址存在否        if (error || placemarks.count == 0) {            self.addressLabel.text = @"该地址无法找到";        }else        {            for (CLPlacemark * placemark in placemarks) {                NSLog(@"name = %@ , locality = %@ , country = %@ , postalCode = %@",placemark.name,placemark.locality,placemark.country,placemark.postalCode);            }            //取出placemarks中的地址对象            CLPlacemark * firstplacemark = [placemarks firstObject];            self.addressLabel.text = [NSString stringWithFormat:@"地址是:%@,纬度是:%.2f,经度是:%.2f",firstplacemark.name,firstplacemark.location.coordinate.latitude,firstplacemark.location.coordinate.longitude];        }    }];}- (IBAction)clickTwo {    //获取文本框中的输入信息    NSString * longtitudeTest = self.longtitudeField.text;    NSString * altitudeTest = self.altitudeField.text;    if (longtitudeTest.length == 0 || altitudeTest.length == 0) {        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入经纬度" preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            NSLog(@"");        }];        [alertController addAction:alertAction];        //不调用这个方法会报错        [self presentViewController:alertController animated:YES completion:nil];    }    //位置    CLLocation * location = [[CLLocation alloc]initWithLatitude:[altitudeTest doubleValue] longitude:[longtitudeTest doubleValue]];    //-------------反地理编码:经纬度转为地址-----------    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {        if (error || placemarks.count == 0) {            self.addressLabel.text = @"该地址无法找到";        }        self.addressLabel.text = [NSString stringWithFormat:@"%@",[placemarks firstObject].name];    }];}//懒加载地理编码类-(CLGeocoder *)geocoder{    if (!_geocoder) {        self.geocoder = [[CLGeocoder alloc]init];    }    return _geocoder;}- (void)viewDidLoad {    [super viewDidLoad];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

注意

搜索的所有结果都是在中国境内的,因为苹果在中国的地图服务商是高德地图

0 0
原创粉丝点击