我的需求是個很簡單的需求:
- 定位自己當(dāng)前的位置胶逢,顯示出自己位置地址
- 顯示出當(dāng)前氣溫和天氣狀況
首先按照高德文檔里的院刁,**pod 'AMapLocation' **唱遭,這個命令還會引入基礎(chǔ)SDK滥嘴,然后pod install县耽,下圖是在高德官方文檔里的截圖:
權(quán)限配置.png
然后就是接著申請你的key动羽,
申請.png
大家作為開發(fā)者都知道bundleid的重要性包帚,嚴格區(qū)分大小寫,寫錯后面的就得不到預(yù)期結(jié)果了运吓。
獲取到key之后渴邦,首先在AppDelegate這個類里疯趟,添加上你的Key,
key.png
還有一個重要的就是在這個方法里- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions添加一句
重要.png
然后就是在需要顯示位置的地方展示,因為我這里是在首頁上顯示谋梭,為了避免在同一個控制器里太多代碼信峻,于是寫了一個category,將這個需求分出來瓮床。代碼如下:
#import "HomepageViewController.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import "WeatherModel.h"
#define WeatherKey @"您的天氣key"
@interface HomepageViewController (Gaode)<AMapLocationManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;
- (void)getLocalAdressinformation;
@end
#import "HomepageViewController+Gaode.h"
@implementation HomepageViewController (Gaode)
- (void)setLocationManager:(AMapLocationManager *)locationManager {
}
- (AMapLocationManager *)locationManager{
// 定位獲取位置
static AMapLocationManager *temp = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
temp = [[AMapLocationManager alloc]init];
temp.delegate = self;
// 此方法開啟持續(xù)定位
// [self.locationManager startUpdatingLocation];
// 定位的精度
[temp setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
temp.locationTimeout = 2;
temp.reGeocodeTimeout = 2;
});
return temp;
}
#pragma mark -- Map delegate
- (void)getLocalAdressinformation{
// 帶逆地理(返回坐標(biāo)和地址信息)盹舞。將下面代碼中的 YES 改成 NO ,則不會返回地址信息隘庄。
[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
if (error)
{
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
if (error.code == AMapLocationErrorLocateFailed)
{
return;
}
}
NSLog(@"location:%@", location);
if (regeocode)
{
NSLog(@"reGeocode:%@", regeocode);
self.navHomeViewController.location.text = regeocode.district;
// 查詢天氣
NetworkRequestManager *requestMgr = [NetworkRequestManager shareInstance];
[requestMgr CurrentWeather:regeocode.adcode AndKey:WeatherKey successblock:^(id responseObject) {
if ([responseObject[@"status"] integerValue]==1) {
NSLog(@"tianqi = %@",responseObject[@"lives"]);
NSArray *temp = [WeatherModel mj_objectArrayWithKeyValuesArray:responseObject[@"lives"]];
for (WeatherModel *wModel in temp) {
self.navHomeViewController.tempValue.text = wModel.temperature;
self.navHomeViewController.weatherState.text = wModel.weather;
}
}
} failuerBlock:^(NSError *error) {
[MBProgressHUD showError:@"獲取天氣信息失敗"];
}];
}
}];
}
里面需要說一下的就是需要查詢到天氣踢步,要另外到高德里申請一個天氣的key,然后按照里面的文檔丑掺,大家就能好獲取到自己想要的東西啦获印。
末尾附上做這兩個簡單功能的參考地址:
[]:http://lbs.amap.com/api/ios-location-sdk/summary/iOS定位SDK
[]:http://lbs.amap.com/api/webservice/guide/api/weatherinfo/#instructionsWeb服務(wù)API