高德地图poi周边搜索

作者: CoderZNB | 来源:发表于2017-01-23 19:57 被阅读5185次

项目截图

高德POI.gif

项目需求

1.公司要求模仿微信/QQ的定位选址效果
2.根据关键字进行定位选址
本项目中用到了高德地图的POI周边搜索功能、单次定位功能、关键字搜索


本人对高德的API进行了一个简单的封装,SLocationManager定位管理者,可以进行单次搜索,持续搜索,关键字搜索,获取当前的经纬度...如果有什么不明白的地方直接看官方文档,文档真的很详细,注释什么的很详细,这个工具类是直接从项目中拷贝过来的,由于工作时间紧张,所以没有demo,😂,小伙伴们不要问demo了,哈哈.👇的快速通道可以帮助小伙伴们快速熟悉高德API,如果有什么疑问,欢迎提问


快速通道

1.根据关键字检索POI(Point of Interest,兴趣点)

关键字检索介绍

根据关键字检索适用于在某个城市搜索某个名称相关的POI,例如:查找北京市的“肯德基”。
注意:关键字未设置城市信息(默认为全国搜索)时,如果涉及多个城市数据返回,仅会返回建议城市,请根据APP需求,选取城市进行搜索。
进入通道➡️


2.检索周边POI

周边检索介绍

适用于搜索某个位置附近的POI,可设置POI的类别,具体查询所在位置的餐饮类、住宅类POI,例如:查找天安门附近的厕所等等场景。
****说明****:

  • 1、支持指定查询POI的类别。高德地图的POI类别共20个大类,分别为:汽车服务、汽车销售、汽车维修、摩托车服务、餐饮服务、购物服务、生活服务、体育休闲服务、医疗保健服务、住宿服务、风景名胜、商务住宅、政府机构及社会团体、科教文化服务、交通设施服务、金融保险服务、公司企业、道路附属设施、地名地址信息、公共设施,同时,每个大类别都还有二级以及三级的细小划分,具体的POI类别请参考:POI分类编码表。
  • 2、不设置POI的类别,默认返回“餐饮服务”、“商务住宅”、“生活服务”这三种类别的POI。
    进入通道➡️

SLocationManager.h


#import <Foundation/Foundation.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

typedef void(^SLocationManagerDidUpdateLocationHandle)(CLLocation *newLocation, NSString *newLatitude, NSString *newLongitude);
typedef void (^SLocationManagerDidGetPOISearchResponse)(AMapPOISearchResponse *response);
typedef void (^SLocationManagerDidGetTipsResponse)(AMapInputTipsSearchResponse *response);
typedef void (^SLocationManagerErrorResponse)(NSString *response);
@interface SLocationManager : NSObject

/** 开始定位*/
- (void)startSerialLocation;
/**开始单次定位*/
- (void)startSingleLocation;
// 快速创建一个locationManager
+ (instancetype)sharedManager;
/** 开始周边搜索🔍 */
- (void)beginAroundSearch;

/**
 搜索更多周边信息

 @param page 搜索的页数
 */
- (void)aroundSearchMore:(NSInteger)page;

/** 更新定位回调*/
- (void)setUpLocationManagerUpdateLocationHandle:(SLocationManagerDidUpdateLocationHandle)updateLocationHandle;
/** 定位到兴趣点 */
- (void)locationManagerDidGetPOISearchResponse:(SLocationManagerDidGetPOISearchResponse)POISearchResponse;
/** 获取到提示搜索词 */
- (void)locationManagerDidGetTipsResponse:(SLocationManagerDidGetTipsResponse)tipsResponse;

/**
 关键字搜索

 @param keywords 搜索关键字 必传参数
 @param city 搜索城市
 @param page 搜索页数
 */
- (void)keywordsSearchWithKeywords:(NSString *)keywords city:(NSString *)city page:(NSInteger)page;
/** 提示搜索 */
- (void)tipsSearchWithKeywords:(NSString *)key city:(NSString *)city ;
/** 是否可以定位*/
@property (nonatomic, assign) BOOL canLocationFlag;

/** 是否有经纬度*/
@property (nonatomic, assign) BOOL hasLocation;

/** 经纬度 */
- (NSString *)curLatitudeAndLongitude;
@end

SLocationManager.m

#import "SLocationManager.h"


@interface SLocationManager ()<AMapSearchDelegate,AMapLocationManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;
@property (strong, nonatomic) AMapSearchAPI *search;

@end


static SLocationManager *_singleton = nil;
@implementation SLocationManager
{
    SLocationManagerDidUpdateLocationHandle _updateLocationHandle;
    SLocationManagerDidGetPOISearchResponse _POISearchResponse;
    SLocationManagerDidGetTipsResponse _tipsResponse;
    BOOL isSearch;
}
- (AMapLocationManager *)locationManager {
    if (!_locationManager) {
        AMapLocationManager *locationManager = [[AMapLocationManager alloc] init];
        _locationManager = locationManager;
        locationManager.delegate = self;
        //iOS 9(不包含iOS 9) 之前设置允许后台定位参数,保持不会被系统挂起
        locationManager.pausesLocationUpdatesAutomatically = NO;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.allowsBackgroundLocationUpdates = NO;
    }
    return _locationManager;
}
- (AMapSearchAPI *)search
{
    if (_search == nil) {
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}
+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _singleton = [[SLocationManager alloc] init];
    });
    return _singleton;
}
- (void)setUpLocationManagerUpdateLocationHandle:(SLocationManagerDidUpdateLocationHandle)updateLocationHandle {
    _updateLocationHandle = updateLocationHandle;
}

- (void)locationManagerDidGetPOISearchResponse:(SLocationManagerDidGetPOISearchResponse)POISearchResponse {

    _POISearchResponse = POISearchResponse;
}
// 开始定位
- (void)startSerialLocation {
    
    if (self.canLocationFlag == NO) {
        return ;
    }
   
    
    //iOS 9(包含iOS 9)之后新特性:将允许出现这种场景,同一app中多个locationmanager:一些只能在前台定位,另一些可在后台定位,并可随时禁止其后台定位。
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
        self.locationManager.allowsBackgroundLocationUpdates = YES;
    }
    //开始持续定位
    [self.locationManager startUpdatingLocation];
}

// 停止定位
- (void)stopSerialLocation {
    [self.locationManager stopUpdatingLocation];
}

// 开始单次定位
- (void)startSingleLocation {
    
    if (self.canLocationFlag == NO) {
        return ;
    }
    // 带逆地理(返回坐标和地址信息)。将下面代码中的 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;
            }
        }
        
        [self saveLocation:location];
        
        if (regeocode)
        {
            NSLog(@"reGeocode:%@", regeocode);
        }
    }];
}
#pragma mark - AMapLocationManager
// 定位错误
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error = %@",  error);
}

// 定位结果
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)newLocation reGeocode:(AMapLocationReGeocode *)reGeocode{
    
    [self saveLocation:newLocation];
   
    [self stopSerialLocation];
}
- (void)saveLocation:(CLLocation *)newLocation {
    // 纬度
    NSString *currentLatitude = [[NSString alloc]
                                 initWithFormat:@"%f",
                                 newLocation.coordinate.latitude];
    
    // 经度
    NSString *currentLongitude = [[NSString alloc]
                                  initWithFormat:@"%f",
                                  newLocation.coordinate.longitude];
    
    [[NSUserDefaults standardUserDefaults] setObject:currentLatitude forKey:kSUserCurrentLatitude];
    [[NSUserDefaults standardUserDefaults] setObject:currentLongitude forKey:kSUserCurrentLongitude];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    if (_updateLocationHandle) {
        _updateLocationHandle(newLocation, currentLatitude, currentLongitude);
    }
    
    if (isSearch) {
        [self beginAroundSearch];
    }
}
/** 经纬度 */
- (NSString *)curLatitudeAndLongitude {

    NSString *latitude = [[NSUserDefaults standardUserDefaults] valueForKey:kSUserCurrentLatitude];
    NSString *longitude = [[NSUserDefaults standardUserDefaults] valueForKey:kSUserCurrentLongitude];
    return [NSString stringWithFormat:@"%@,%@",latitude,longitude];
}
- (BOOL)isCanLocationFlag {
    
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse)) {
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)hasLocation {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLongitude] length] && [[[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLatitude] length];
}
#pragma mark - 周边搜索
- (void)beginAroundSearch {

    if (![self hasLocation]) {
        isSearch = YES;
        [self startSingleLocation];
        return;
    }
    NSString *currentLatitude = [[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLatitude];
    NSString *currentLongitude = [[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLongitude];
    
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    
    request.location            = [AMapGeoPoint locationWithLatitude:[currentLatitude floatValue] longitude:[currentLongitude floatValue]];
    request.keywords            = @"";
    /* 按照距离排序. */
    request.sortrule            = 0;
    request.requireExtension    = YES;
    
    [self.search AMapPOIAroundSearch:request];
}
#pragma mark - AMapSearchDelegate

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
//    if (response.pois.count == 0){
//        
//        return;
//    }
    for(AMapPOI *poi in response.pois){
        NSLog(@"%@.%@-%@-%@",poi.name,poi.district,poi.businessArea,poi.address);
    }
    
    if (_POISearchResponse) {
        _POISearchResponse(response);
    }
}
- (void)aroundSearchMore:(NSInteger)page {
    
    NSString *currentLatitude = [[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLatitude];
    NSString *currentLongitude = [[NSUserDefaults standardUserDefaults] objectForKey:kSUserCurrentLongitude];
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    
    request.location            = [AMapGeoPoint locationWithLatitude:[currentLatitude floatValue] longitude:[currentLongitude floatValue]];
    request.keywords            = @"";
    /* 按照距离排序. */
    request.sortrule            = 0;
    request.requireExtension    = YES;
    request.page = page;
    [self.search AMapPOIAroundSearch:request];
}
#pragma mark - 关键字搜索
- (void)keywordsSearchWithKeywords:(NSString *)keywords city:(NSString *)city page:(NSInteger)page{
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    
    request.keywords            = keywords;
    request.city                = city;
//    request.types               = @"高等院校";
    request.requireExtension    = YES;
    
    /*  搜索SDK 3.2.0 中新增加的功能,只搜索本城市的POI。*/
//    request.cityLimit           = YES;
//    request.requireSubPOIs      = YES;
    request.page = page;
    [self.search AMapPOIKeywordsSearch:request];
}

#pragma mark - 提示搜索
- (void)tipsSearchWithKeywords:(NSString *)key city:(NSString *)city {
    AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
    tips.keywords = key;
    tips.city     = city;
    [self.search AMapInputTipsSearch:tips];
}

/* 输入提示回调. */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
//    if (response.tips.count == 0) {
//        return;
//    }

    if (_tipsResponse) {
        _tipsResponse(response);
    }
}

- (void)locationManagerDidGetTipsResponse:(SLocationManagerDidGetTipsResponse)tipsResponse {
    _tipsResponse = tipsResponse;
}
@end

相关文章

网友评论

  • 陌上北辰:我想问下,为什么(-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response)我每次返回的数据都只有10条呢,可以设置吗
    HaoKe:// 设置每页最多返回多少条poiitem
    query.setPageSize(30);
    HaoKe:int currentPage = 0;
    //不输入城市名称有些地方搜索不到
    // 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
    query = new PoiSearch.Query(key, "", "");
    // 设置每页最多返回多少条poiitem
    query.setPageSize(30);
    // 设置查询页码
    query.setPageNum(currentPage);

    //构造 PoiSearch 对象,并设置监听
    poiSearch = new PoiSearch(this, query);
    poiSearch.setOnPoiSearchListener(this);
    //调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。
    poiSearch.searchPOIAsyn();
    CoderZNB:@陌上北辰我记得可以的,文档里有说
  • 1ffdfbe42a4c:您好!想问下现在有demo么?或者帮助一下qq:946608155,万分感谢!
    1ffdfbe42a4c:@CoderZNB 而且我的需求几乎跟楼主的一模一样,所以希望楼主能多给一些帮助,我卡在这两天了
    1ffdfbe42a4c:@CoderZNB 我集成啥的都成功了,但是咋就出不来列表呢,而且还竟然走if (self.canLocationFlag == NO) {
    return ;
    }这个了,这个不是没定位成功么
    CoderZNB:没有demo啊,你走到哪一步了
  • CodeMe:请问AMapPOIAroundSearchRequest支持拼音搜索吗
    CoderZNB:@CodeMe 那你就得在返回的结果里处理了,遍历所有的结果,使用属性字符串,给关键字设置高亮
    CodeMe:@CoderZNB 如何像微信那样,将包含关键字的部分高亮,即使用拼音也可以
    CoderZNB:可以的
  • Locaus_Joe:请问我调用了[[SLocationManager sharedManager] beginAroundSearch],没有返回周围搜索的数据呢?
    Locaus_Joe:@ZNBmm 是的 已经弄好 666
    CoderZNB: @Locaus_Joe - (void)locationManagerDidGetPOISearchResponse,这个方法里返回了,如果我没记错的话😬
  • Vampire_Jam:真不赖啊你

本文标题:高德地图poi周边搜索

本文链接:https://www.haomeiwen.com/subject/qwwabttx.html