您的当前位置:首页正文

iOS集成高德地图和天气的简易应用

来源:图艺博知识网

我的需求是个很简单的需求:

  • 定位自己当前的位置,显示出自己位置地址
  • 显示出当前气温和天气状况

首先按照高德文档里的,**pod 'AMapLocation' **,这个命令还会引入基础SDK,然后pod install,下图是在高德官方文档里的截图:


权限配置.png

然后就是接着申请你的key,


申请.png
大家作为开发者都知道bundleid的重要性,严格区分大小写,写错后面的就得不到预期结果了。
获取到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;
        // 此方法开启持续定位
        // [self.locationManager startUpdatingLocation];
        
        // 定位的精度
        [temp setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
        temp.locationTimeout  = 2;
        temp.reGeocodeTimeout = 2;
    });

    return temp;
    
}
#pragma mark -- Map delegate
- (void)getLocalAdressinformation{
    
    // 带逆地理(返回坐标和地址信息)。将下面代码中的 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,然后按照里面的文档,大家就能好获取到自己想要的东西啦。

Top