百度地图反地理

1、地理编码指的是将地址位置(中文地址)转换成经纬度,反地址编码指的是将经纬度转换成地址位置;

2、在百度地图中需要用到三个关键性的类:BMKGeoCodeSearch、BMKGeoCodeSearchOption、BMKReverseGeoCodeOption;

3、BMKGeoCodeSearch:地理编码主类,用来查询、返回结果信息(地址位置或经纬度);

4、BMKGeoCodeSearchOption:地理编码选项,即地理编码的数据模型,地址是通过该类传递进去的;

5、BMKReverseGeoCodeOption:反地理编码选项,即反地理编码的数据模型,经纬度就是通过该类传递进去的;

6、有了以上基本信息,开始做一个简单的示例:从手机页面上输入经纬度通过按钮事件将对应的地理位置输出到手机屏幕,反之亦然;

7、基本UI视图如下所示:


8、关键代码:

    1. //
    2. //  SAViewController.m
    3. //  MapDemo
    4. //
    5. //  Created by Sian on 14/11/13.
    6. //  Copyright (c) 2014年 Sian. All rights reserved.
    7. //

    8. #import "SAViewController.h"
    9. #import "BMapKit.h"

    10. @interface SAViewController () <BMKGeoCodeSearchDelegate>

    11. @property (nonatomic, strong) BMKGeoCodeSearch *geoCode;        // 地理编码

    12. @property (weak, nonatomic) IBOutlet UITextField *longitude;    // 经度输入
    13. @property (weak, nonatomic) IBOutlet UITextField *latitude;     // 纬度输入
    14. @property (weak, nonatomic) IBOutlet UILabel *address;          // 位置输出

    15. @property (weak, nonatomic) IBOutlet UITextField *inputAddress; // 地址输入
    16. @property (weak, nonatomic) IBOutlet UILabel *location;         // 经纬输出

    17. // 地址输出事件
    18. - (IBAction)outputAdd;
    19. // 经纬度输出事件
    20. - (IBAction)outputLocation;
    21. @end

    22. @implementation SAViewController

    23. - (void)viewDidLoad
    24. {
    25.     [super viewDidLoad];
    26. }

    27. #pragma mark geoCode的Get方法,实现延时加载
    28. - (BMKGeoCodeSearch *)geoCode
    29. {
    30.     if (!_geoCode) {
    31.         _geoCode = [[BMKGeoCodeSearch alloc] init];
    32.         _geoCode.delegate = self;
    33.     }
    34.     return _geoCode;
    35. }

    36. #pragma mark 点击空白处隐藏键盘
    37. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    38. {
    39.     [self.view endEditing:YES];
    40. }

    41. #pragma mark 获取地理位置按钮事件
    42. - (IBAction)outputAdd
    43. {
    44.     // 初始化反地址编码选项(数据模型)
    45.     BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    46.     // 将TextField中的数据传到反地址编码模型
    47.     option.reverseGeoPoint = CLLocationCoordinate2DMake([self.latitude.text floatValue], [self.longitude.text floatValue]);
    48.     NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
    49.     // 调用反地址编码方法,让其在代理方法中输出
    50.     [self.geoCode reverseGeoCode:option];
    51. }

    52. #pragma mark 获取经纬度按钮事件
    53. - (IBAction)outputLocation
    54. {
    55.     // 初始化地址编码选项(数据模型)
    56.     BMKGeoCodeSearchOption *option = [[BMKGeoCodeSearchOption alloc] init];
    57.     // 将TextField中的数据传到地址编码模型
    58.     option.address = self.inputAddress.text;
    59.     NSLog(@"%@", option.address);
    60.     // 调用地址编码方法,让其在代理方法中输出
    61.     [self.geoCode geoCode:option];
    62. }

    63. #pragma mark 代理方法返回反地理编码结果
    64. - (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
    65. {
    66.     if (result) {
    67.         self.address.text = [NSString stringWithFormat:@"%@", result.address];
    68.         NSLog(@"%@ - %@", result.address, result.addressDetail.streetNumber);
    69.     }else{
    70.         self.address.text = @"找不到相对应的位置信息";
    71.     }
    72. }

    73. #pragma mark 代理方法返回地理编码结果
    74. - (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
    75. {
    76.     if (result) {
    77.         self.location.text = [NSString stringWithFormat:@"经度为:%.2f   纬度为:%.2f", result.location.longitude, result.location.latitude];
    78.         NSLog(@"%@", result.address);
    79.     }else{
    80.         self.location.text = @"找不到相对应的位置";
    81.     }
    82. }
    83. @end
原文地址:https://www.cnblogs.com/keyan1102/p/4309262.html