最新有需求要获取当前地址的坐标以及当前城市的地理编码。在网上找例子找了一堆都木有说明白该怎么搞!(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 (