iOS 高德地图 获取当前城市的地理编码:(like 0755 )

本文介绍如何在iOS应用中使用高德地图获取当前坐标和城市地理编码。首先需要在高德地图官网申请API key,然后在AppDelegate中注册,并在UIViewController中设置相关类。通过特定方法获取经纬度和中文地址,注意地理编码查询需额外申请web服务的key,且请求方式必须为GET。解析返回数据,即可得到所需的城市编码。
摘要由CSDN通过智能技术生成

最新有需求要获取当前地址的坐标以及当前城市的地理编码。在网上找例子找了一堆都木有说明白该怎么搞!(WTF....)最后还是在高德地图官网设置里面搞清楚了,废话不多说,详见代码!(当然地图的第三方包自己去官网下载哈~)

获取坐标前提,你的去官网申请一个key,选择好需要用到的平台,当然 我这里是iOS ,具体就不细说了,有了key,可以进入下一步。

第一步: 配置一个 h 类型的key文件(后面方便调用),key就是你在官网获取的key值:

官网地址:http://lbs.amap.com/dev/key/app

#ifndef OfficialDemoLoc_APIKey_h

#define OfficialDemoLoc_APIKey_h



/* 使用高德地图API,请注册Key,注册地址:http://lbs.amap.com/console/key */



const static NSString *APIKey = @"你的key!";



#endif

 

然后,需要在你的AppDelegate里面先注册一下。
 

- (void)configureAPIKey

{

    if ([APIKey length] == 0)

    {

        NSString *reason = [NSString stringWithFormat:@"apiKey为空,请检查key是否正确设置。"];

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:reason delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];

        

        [alert show];

    }

    

    [AMapServices sharedServices].apiKey = (NSString *)APIKey;

}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

    [self configureAPIKey];//高德地图app注册

    LoginViewController *Login =  [[LoginViewController alloc] init];

    self.window.rootViewController = Login;

    

    [self.window makeKeyAndVisible];

    

    return YES;

}

再然后在你UIViewController.h文件中写好需要调用的类:

#import <UIKit/UIKit.h>

#import <MAMapKit/MAMapKit.h>

#import <AMapLocationKit/AMapLocationKit.h>



@interface LocationViewController : UIViewController



@property (nonatomic, strong) MAMapView *mapView;



@property (nonatomic, strong) AMapLocationManager *locationManager;



@end

and 我们就可以开始愉快的写方法了。

获取当前坐标:

@interface LocationViewController ()<MAMapViewDelegate, AMapLocationManagerDelegate>

@property (nonatomic, strong) UISegmentedControl *showSegment;

@property (nonatomic, strong) UISegmentedControl *backgroundSegment;

@property (
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值