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
- iOS9下有关CoreLocation学习三
- iOS9下有关CoreLocation学习一
- iOS9下有关CoreLocation学习二
- CoreLocation框架地图定位(iOS9全适配)
- CoreLocation定位学习笔记
- iOS8.0+ 至 iOS9.0之前的CoreLocation定位
- iOS8下,CoreLocation的变化
- CoreLocation
- CoreLocation
- CoreLocation
- coreLocation
- Swift 学习笔记(二)Ios9下访问http://
- iOS CoreLocation框架第一章—— CLLocationManager的使用(包含iOS6,iOS8,iOS9各种变动)
- ios9学习系列:UIStackView
- iOS学习笔记 地图(一)定位CoreLocation
- 地图---CoreLocation框架(学习笔记)
- iOS9-by-Tutorials-学习笔记三:Your-App-on-the-Web
- iOS9下http及https
- 常见C++内存池技术
- Clob 转String 类型使用 以及转码问题
- iphone分辨率终极指南(含iphone6/6+)
- system阻塞SIGCHLD信号原因
- Socket使用教程——AsyncSocket
- iOS9下有关CoreLocation学习三
- Codeforces 416E. President's Path 图论 最短路 处理技巧
- 《数据结构与算法分析(c 描述)》—— 第六章笔记
- NS2在eclipse下的部署以及调试
- xcode 调试
- 华尔街见闻-2016年2月
- Android allowTaskReparenting clearTaskOnLaunch alwaysRetainTaskState
- ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
- hdu4521 小明系列问题——小明序列(条件LIS)