码迷,mamicode.com
首页 > 其他好文 > 详细

进阶篇第十一期:高德地图的使用

时间:2015-05-24 19:05:41      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:ios


不知道小伙伴们有木有用高德的API来玩高德地图,其实还好啦,API里面介绍的很详细了哦,那么我今天就介绍一些常用的吧


首先是导入SDK,我用的是2D地图,所以就导入两个2D的库,那么我用CocoaPod来管理


 
pod ‘AMap2DMap‘
pod ‘AMapSearch‘


接下来我们在调用地图的Controller里面来导入需要用的类库,并且关联对应的delegate


 
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

<MAMapViewDelegate, AMapSearchDelegate>


然后我们来初始化地图,将已经申请好的key填进去

这里注意要记得iOS8后要再info.plist中添加详细字段哦


 
- (void)loadMapView {
    [MAMapServices sharedServices].apiKey = @"fe17d6556c1896ead41ef2e25dc724a8";
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectZero];
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
//    [self.view addSubview:self.mapView];
    //在info.plist中添加字段NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription
}


然后实现对应的代理方法


 
#pragma mark - MAMapViewDelegate

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    if (updatingLocation) {
        NSLog(@"%f, %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
    }
}

- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
    
}


接下来介绍一下搜索,由于官方文档介绍的并不是很详细,搜索一个字的时候没有处理,那么我们自己来处理一下好啦


 
#pragma mark - MapSearchAPI

- (void)searchMapResultWithString:(NSString *)string {
    AMapPlaceSearchRequest *mapPlaceSearchRequest = [[AMapPlaceSearchRequest alloc] init];
    mapPlaceSearchRequest.searchType = AMapSearchType_PlaceKeyword;
    mapPlaceSearchRequest.keywords = string;
    if (self.currentCity.count && self.currentCity) {
        AMapCity *mapCity = (AMapCity *)self.currentCity[0];
        mapPlaceSearchRequest.city = @[mapCity.city];
    }
    mapPlaceSearchRequest.requireExtension = YES;
    [self.mapSearch AMapPlaceSearch:mapPlaceSearchRequest];
}

- (void)loadSearchMapApi {
    self.mapSearch = [[AMapSearchAPI alloc] initWithSearchKey:@"fe17d6556c1896ead41ef2e25dc724a8" Delegate:self];
    self.mapSearch.language = AMapSearchLanguage_zh_CN;
    self.currentCity = [[NSArray alloc] init];
    [self searchMapResultWithString:@"你"];
}
 
#pragma mark - AMapSearchDelegate

- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response {
    if (!response.pois.count && response.suggestion.cities.count) {
        self.currentCity = response.suggestion.cities;
        [self searchMapResultWithString:@"你"];
        
        return;
    }
    
    NSLog(@"%@", [response.pois description]);
}




OK,这就是我们今天要介绍的高德地图的基本使用了哦



本文出自 “东软iOS校友群的技术博客” 博客,请务必保留此出处http://neusoftios.blog.51cto.com/9977509/1654677

进阶篇第十一期:高德地图的使用

标签:ios

原文地址:http://neusoftios.blog.51cto.com/9977509/1654677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!