IOS开发-高德地图POI开发

IOS开发-高德地图POI开发

高德地图POI,可以为我们提供周边兴趣点。

##POI搜索

  • 关键字搜索

关键字搜索,就是一个比较精准的地名搜索,比如:我们在地图上搜索一个地名:故宫博物院,故宫的位置会在地图上标注出来;

  • 周边搜索

周边搜索,就是我们在各个APP上最常见的“附近”功能,比如:我现在位于杭州西湖,想看看周边有什么好吃的餐馆,点击“附近”好吃的餐馆就都在地图上标注出来了;

  • 多边形搜索

多边形搜索也不难理解,很多时候我们搜索的范围不是一个规则的圆形,而是一个边缘线不规则的区域,比如:我是一个商业调查公司,想搜索望京商圈里,有多少座写字楼,就可以使用多边形搜索功能,划定一个不规则范围,在该范围内进行搜素;

##IOS方面使用高德的图POI

  • 1、使用高德地图服务,首先要先申请key

  • 2、创建AMapServiceManager

AMapServiceManager.h


#import <Foundation/Foundation.h>

@interface AMapServiceManager : NSObject

+ (instancetype)sharedInstance;

- (void)startService;

@end

AMapServiceManager.m


#import "AMapServiceManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>

#define kAMapServiceKey @“你申请的高德地图的apiKey”

static AMapServiceManager *shareInstance = nil;

@implementation AMapServiceManager

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[AMapServiceManager alloc] init];
    });
    return shareInstance;
}

- (void)startService {
    [AMapServices sharedServices].apiKey = kAMapServiceKey;
}


@end

  • 3、AppDelegate中启动服务

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.    
    [[AMapServiceManager sharedInstance] startService];
    
    [self.window makeKeyAndVisible];
    return YES;
}

  • 4、创建POIViewController

POIViewController.h


#import "BaseViewController.h"

@interface AMapPOIViewController : BaseViewController

@end

POIViewController.m


#import "AMapPOIViewController.h"
#import "AMapPOIView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "AMapPOIListCell.h"
#import "MJRefresh.h"

static NSInteger pageCount = 10;

@interface AMapPOIViewController ()<MAMapViewDelegate,UITableViewDelegate,UITableViewDataSource,AMapSearchDelegate>

@property (nonatomic,strong) AMapPOIView *amapPOIView;
@property (nonatomic,strong) AMapSearchAPI *mapSearch;
@property (nonatomic,strong) NSMutableArray *poiList;
@property (nonatomic,assign) NSInteger currentPageIndex;
@property (nonatomic,assign) BOOL isInLoading;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate2D;

@end

@implementation AMapPOIViewController

- (NSMutableArray *)poiList {
    if (!_poiList) {
        _poiList = [NSMutableArray arrayWithCapacity:0];
    }
    return _poiList;
}

- (id)init {
    self = [super init];
    if (self) {
        
        self.currentPageIndex = 1;
        self.isInLoading = NO;
    }
    return self;
}

- (void)loadView {
    [super loadView];
    _amapPOIView = [[AMapPOIView alloc] init];
    _amapPOIView.delegate = self;
    self.view = _amapPOIView;
    
    _amapPOIView.poiHeaderView.mapView.showsUserLocation = YES;
    _amapPOIView.poiHeaderView.mapView.zoomLevel = 10;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mapSearch = [[AMapSearchAPI alloc] init];
    self.mapSearch.delegate = self;
    [self amapSearchPOIByKeyWords:@""];
    
    // 下拉刷新
    __weak typeof(self) weakSelf = self;
    self.amapPOIView.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
        [weakSelf refreshMapPOIData];
    }];
    
    // 上拉刷新
    self.amapPOIView.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 模拟延迟加载数据,因此2秒后才调用(真实开发中,可以移除这段gcd代码)
        [weakSelf loadMoreMapPOIData];
    }];
}

#pragma mark MJRefresh Methods
- (void)refreshMapPOIData {
    self.currentPageIndex = 1;
    [self loadPOI];
}

- (void)loadMoreMapPOIData {
    [self loadPOI];
}

- (void)loadPOI {
    if (self.isInLoading) {
        return;
    }
    self.isInLoading = YES;
    [self amapSearchPOIByKeyWords:@""];
}

- (void)endRefreshing {
    self.isInLoading = NO;
    [self.amapPOIView.tableView.mj_header endRefreshing];
    [self.amapPOIView.tableView.mj_footer endRefreshing];
}

#pragma mark TableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.poiList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"aMapPOIListCell";
    AMapPOIListCell *cell = (AMapPOIListCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[AMapPOIListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.title = [self.poiList objectAtIndex:indexPath.row];
    cell.showLine = YES;
    if (indexPath.row == (self.poiList.count - 1)) {
        cell.showLine = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
}

#pragma mark - AMapSearchPOI

- (void)amapSearchPOIByKeyWords:(NSString *)keyword {
    
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    
    request.location                    = [AMapGeoPoint locationWithLatitude:self.coordinate2D.latitude longitude:self.coordinate2D.longitude];
    request.keywords                    = keyword;
    request.sortrule                    = 0;
    request.requireExtension            = YES;
    request.radius                      = 1000;
    request.page                        = self.currentPageIndex;
    request.offset                      = pageCount;
    request.types                       = @"050000|060000|070000|080000|090000|100000|110000|120000|130000|140000|150000|160000|170000";
    request.requireExtension    = YES;
    request.sortrule            = 0;    //按照距离排序
    [self.mapSearch AMapPOIAroundSearch:request];
}


#pragma mark - AMapSearchDelegate

- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error {
    [self endRefreshing];
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response {
    
    [self endRefreshing];
    
    if (response.pois.count == 0) {
        return;
    }
    
    if (self.currentPageIndex == 1) {
        [self.poiList removeAllObjects];
    }
    
    [response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
        [self.poiList addObject:obj.name];
        [self.amapPOIView.tableView reloadData];
    }];
    
    self.currentPageIndex++;
    [self endRefreshing];
}

#pragma mark - MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
    
}

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate;
    NSLog(@"centerCoordinate:(%f,%f)",centerCoordinate.latitude,centerCoordinate.longitude);
    self.coordinate2D = centerCoordinate;
    [self refreshMapPOIData];
}

- (void)mapView:(MAMapView *)mapView mapWillMoveByUser:(BOOL)wasUserAction {
    
}

- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction {
    
}

- (void)mapView:(MAMapView *)mapView mapWillZoomByUser:(BOOL)wasUserAction {
    
}

- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 5、创建AMapPOIView

AMapPOIView.h

#import <UIKit/UIKit.h>
#import "AMapPOIHeaderView.h"

@interface AMapPOIView : UIView

@property (nonatomic, weak) id delegate;
@property (nonatomic, strong) AMapPOIHeaderView *poiHeaderView;
@property (nonatomic, strong) UITableView *tableView;

- (id)initWithFrame:(CGRect)frame;

@end

AMapPOIView.m

#import "AMapPOIView.h"
#import "UIColor+hex.h"
#import "NSString+hex.h"


@interface AMapPOIView ()

@end

@implementation AMapPOIView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
        
        _poiHeaderView = [[AMapPOIHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)/3)];
        [self addSubview:_poiHeaderView];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:_tableView];
    }
    return self;
}

- (id)init {
    return [self initWithFrame:CGRectZero];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.poiHeaderView.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), CGRectGetHeight([UIScreen mainScreen].bounds)/3);
    self.tableView.frame = CGRectMake(0.0, CGRectGetMaxY(self.poiHeaderView.frame), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)-CGRectGetMaxY(self.poiHeaderView.frame));
}

- (void)setDelegate:(id)delegate {
    _delegate = delegate;
    _tableView.delegate = delegate;
    _tableView.dataSource = delegate;
    _poiHeaderView.delegate = delegate;
}

@end

  • 6、显示地图的View:AMapPOIHeaderView

AMapPOIHeaderView.h

#import <UIKit/UIKit.h>
#import <MAMapKit/MAMapKit.h>

@interface AMapPOIHeaderView : UIView

@property (nonatomic, weak) id delegate;
@property (nonatomic, strong) MAMapView *mapView;

@end


AMapPOIHeaderView.m


#import "AMapPOIHeaderView.h"
#import "UIColor+hex.h"

@interface AMapPOIHeaderView ()

@end

@implementation AMapPOIHeaderView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
        
        ///初始化地图
        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
        [self addSubview:_mapView];
    }
    return self;
}

- (id)init {
    return [self initWithFrame:CGRectZero];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.mapView.frame = self.bounds;
}

- (void)setDelegate:(id)delegate {
    _delegate = delegate;
    _mapView.delegate = delegate;
}

- (void)dealloc {
    
}

@end


##高德提供的POI搜索的代码如下

/* 根据ID来搜索POI. */
- (void)searchPoiByID
{
    AMapPOIIDSearchRequest *request = [[AMapPOIIDSearchRequest alloc] init];
 
    request.uid                 = @"B000A7ZQYC";
    request.requireExtension    = YES;
     
    [self.search AMapPOIIDSearch:request];
     
}
 
/* 根据关键字来搜索POI. */
- (void)searchPoiByKeyword
{
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
     
    request.keywords            = @"北京大学";
    request.city                = @"北京";
    request.types               = @"高等院校";
    request.requireExtension    = YES;
     
    /*  搜索SDK 3.2.0 中新增加的功能,只搜索本城市的POI。*/
    request.cityLimit           = YES;
    request.requireSubPOIs      = YES;
     
    [self.search AMapPOIKeywordsSearch:request];
}


本文作为学习记录,以便之后查阅。谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值