iOS開(kāi)發(fā)之高德地圖

之前的項(xiàng)目用到了高德地圖sdk,在此對(duì)于定位和逆地理編碼,天氣查詢(xún)等功能做一個(gè)總結(jié),有什么不正確的地方,歡迎提出來(lái)。
使用高德地圖,首先需要申請(qǐng)一個(gè)key,申請(qǐng)流程按照官網(wǎng)的提示就可以。導(dǎo)入相應(yīng)的第三方庫(kù),申請(qǐng)好key值后,需要在application里配置用戶(hù)的key值.
[AMapServices sharedServices].apiKey = @"你的key值";
注意:有時(shí)候這里設(shè)置了key值運(yùn)行程序還會(huì)出現(xiàn)key值為空或者驗(yàn)證失敗的提示,我也不知道為什么,不過(guò)在viewDidLoad再把這句話(huà)寫(xiě)一遍,就可以解決這個(gè)問(wèn)題,如果有人知道原因,還望告訴我一下。
配置了key值我們就可以使用高德提供的功能了。

地圖

地圖是最基礎(chǔ)的功能,展示一個(gè)地圖也非常簡(jiǎn)單.
注意設(shè)置代理MAMapViewDelegate
@property(nonatomic,strong)MAMapView *mapView;
然后初始化地圖

//設(shè)置mapView的Frame
//這里的kHeight是我定義的整個(gè)屏幕的高度
_mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.view.bounds), kHeight*0.5)];
//地圖視圖加到主視圖
[self.view addSubview:_mapView];
//地圖的縮放
[_mapView setZoomLevel:14.5 animated:YES];
//設(shè)置MapView的委托為自己
self.mapView.delegate = self;

到這里我們運(yùn)行程序就可以看到地圖的顯示了

定位

定位是用到最多的功能,分為單次定位和持續(xù)定位,這里我用的是持續(xù)定位。
依舊需要設(shè)置代理AMapLocationManagerDelegate
@property (nonatomic,strong) AMapLocationManager *locationManager; @property (nonatomic,assign) CLLocationCoordinate2D currentCoordinate;//用來(lái)保存定位到的坐標(biāo)

開(kāi)啟定位
//是否顯示用戶(hù)的位置
self.mapView.showsUserLocation = YES;
//持續(xù)定位
self.locationManager = [[AMapLocationManager alloc] init];
self.locationManager.delegate = self;
//開(kāi)啟持續(xù)定位
[self.locationManager startUpdatingLocation];

定位成功會(huì)調(diào)用

-(void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location{
//輸出的是模擬器的坐標(biāo)
CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
_currentCoordinate = coordinate2D;
_mapView.centerCoordinate = coordinate2D;}

定位失敗后會(huì)調(diào)用

-(void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error{
    //定位錯(cuò)誤
    NSLog(@"定位失敗");
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

天氣

高德同樣可以查詢(xún)天氣情況,不過(guò)官網(wǎng)的文檔寫(xiě)的不夠詳細(xì),這里給出自己的添加的代碼
用天氣查詢(xún)功能需要添加代理AMapSearchDelegate
@property(nonatomic,strong)AMapSearchAPI *search;
因?yàn)橄胪ㄟ^(guò)定位的地點(diǎn)來(lái)查詢(xún)天氣,所以在剛剛定位的回調(diào)函數(shù)里寫(xiě)上天氣搜索的代碼

//輸出的是模擬器的坐標(biāo)
CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
_currentCoordinate = coordinate2D;
//設(shè)置地圖的中心點(diǎn)是定位的點(diǎn)
_mapView.centerCoordinate = coordinate2D;

//搜索天氣
self.search = [[AMapSearchAPI alloc]init];
self.search.delegate = self;
[self searchDistrictWithName];

查詢(xún)天氣功能是用城市名,所以需要逆地理編碼

//搜索天氣
- (void)searchDistrictWithName
{
  AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
  request.location = [AMapGeoPoint locationWithLatitude:_currentCoordinate.latitude longitude:_currentCoordinate.longitude];
  //逆地理編碼搜索請(qǐng)求
  [_search AMapReGoecodeSearch:request];  
}
//實(shí)現(xiàn)逆地理編碼的回調(diào)函數(shù)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
  if(response.regeocode != nil)
{
    //通過(guò)AMapReGeocodeSearchResponse對(duì)象處理搜索結(jié)果
    //NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode.formattedAddress];
    NSString *cityStr = [NSString stringWithFormat:@"%@",response.regeocode.addressComponent.city];
    //NSLog(@"%@",cityStr);
    //查詢(xún)天氣
    AMapWeatherSearchRequest *request = [[AMapWeatherSearchRequest alloc] init];
    request.city = cityStr;
    request.type = AMapWeatherTypeLive; //AMapWeatherTypeLive為實(shí)時(shí)天氣;AMapWeatherTypeForecase為預(yù)報(bào)天氣
    [self.search AMapWeatherSearch:request];  
  }
}

同樣有相應(yīng)的回調(diào)函數(shù)

- (void)onWeatherSearchDone:(AMapWeatherSearchRequest *)request response:(AMapWeatherSearchResponse *)response{

//如果是實(shí)時(shí)天氣
if(request.type == AMapWeatherTypeLive)
{
    if(response.lives.count == 0)
    {
        return;
    }
    for (AMapLocalWeatherLive *live in response.lives) {
        NSLog(@"區(qū)域編碼:%@",live.adcode);
        NSLog(@"省份:--%@",live.province);
        NSLog(@"城市:--%@",live.city);
        NSLog(@"天氣:--%@",live.weather);
        NSLog(@"實(shí)時(shí)溫度:--%@",live.temperature);
        NSLog(@"風(fēng)向:--%@",live.windDirection);
        NSLog(@"風(fēng)力:--%@",live.windPower);
        NSLog(@"空氣濕度:--%@",live.humidity);
        NSLog(@"發(fā)布時(shí)間:--%@",live.reportTime);
    }
}
//如果是預(yù)報(bào)天氣
else
{
    if(response.forecasts.count == 0)
    {
        return;
    }
    for (AMapLocalWeatherForecast *forecast in response.forecasts) {
        NSLog(@"====%@",forecast);
    }
}}

查詢(xún)失敗時(shí)候的回調(diào)

//檢索失敗時(shí)候回調(diào)
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
NSLog(@"Error: %@", error);}

注意:模擬器無(wú)法正確定位,如果想要定位的話(huà)可以通過(guò)虛擬坐標(biāo),即調(diào)出模擬器,然后debug菜單里的location里的custom location,可以輸入相應(yīng)的經(jīng)緯度。

代碼比較粗糙,但也實(shí)現(xiàn)包含地圖,定位,查詢(xún)天氣在內(nèi)的基本功能,希望可以給同樣是新手的朋友們一些幫助。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容