以下为获取POI 数据的一些方法,记录一下。
搜索类
AMapSearchAPI *search = [[AMapSearchAPI alloc] init];
search.delegate = self;
- 根据关键词搜索
关键字检索的请求参数类为 AMapPOIKeywordsSearchRequest,其中 keywords 是必设参数。types 为搜索类型
AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
request.keywords = @"北京大学";
// request.city = @"北京";
request.types = @"高等院校";
// //是否返回扩展信息
// request.requireExtension = YES;
//
// /* 搜索SDK 3.2.0 中新增加的功能,只搜索本城市的POI。*/
// request.cityLimit = YES;
// ///是否返回子POI,默认为 NO。
// request.requireSubPOIs = YES;
调用 AMapSearchAPI 的 AMapPOIKeywordsSearch 并发起关键字检索。
// [self.search AMapPOIKeywordsSearch:request];
- 根据ID搜索
AMapPOIIDSearchRequest *request = [[AMapPOIIDSearchRequest alloc] init];
//POI全局唯一ID
request.uid = uid;
request.requireExtension = YES;
[self.search AMapPOIIDSearch:request];
- 根据中心点坐标来搜周边的POI
AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
request.location = [AMapGeoPoint locationWithLatitude:39.990459 longitude:116.481476];
request.keywords = @"电影院";
/* 按照距离排序. */
request.sortrule = 0;
request.requireExtension = YES;
[self.search AMapPOIAroundSearch:request];
- 在指定的范围内搜索POI.(多边型搜索)
NSArray *points = [NSArray arrayWithObjects:
[AMapGeoPoint locationWithLatitude:39.990459 longitude:116.481476],
[AMapGeoPoint locationWithLatitude:39.890459 longitude:116.581476],
nil];
/**
* @brief 实例化一个多边形对象
* @param points 坐标集, AMapGeoPoint 数组
*/
AMapGeoPolygon *polygon = [AMapGeoPolygon polygonWithPoints:points];
AMapPOIPolygonSearchRequest *request = [[AMapPOIPolygonSearchRequest alloc] init];
//多边形
request.polygon = polygon;
request.keywords = @"Apple";
request.requireExtension = YES;
[self.search AMapPOIPolygonSearch:request];
以上四种(关键字 周边搜索 多边形搜索 ID 搜索)的回调(来自官方demo)
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
if (response.pois.count == 0)
{
return;
}
NSMutableArray *poiAnnotations = [NSMutableArray arrayWithCapacity:response.pois.count];
[response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
[poiAnnotations addObject:[[POIAnnotation alloc] initWithPOI:obj]];
}];
/* 将结果以annotation的形式加载到地图上. */
[self.mapView addAnnotations:poiAnnotations];
/* 如果只有一个结果,设置其为中心点. */
if (poiAnnotations.count == 1)
{
[self.mapView setCenterCoordinate:[poiAnnotations[0] coordinate]];
}
/* 如果有多个结果, 设置地图使所有的annotation都可见. */
else
{
[self.mapView showAnnotations:poiAnnotations animated:NO];
}
}
- 获取道路沿途的POI
AMapRoutePOISearchRequest *request = [[AMapRoutePOISearchRequest alloc] init];
///起点坐标
///AMapGeoPoint 经纬度类
request.origin = [AMapGeoPoint locationWithLatitude:self.startCoordinate.latitude longitude:self.startCoordinate.longitude];
///终点坐标
request.destination = [AMapGeoPoint locationWithLatitude:self.destinationCoordinate.latitude longitude:self.destinationCoordinate.longitude];
request.strategy = self.strategy;
request.searchType = self.segmentControl.selectedSegmentIndex;
发起沿途搜索
[self.search AMapRoutePOISearch:request];
沿途搜索回调
/* 沿途搜索回调. */
- (void)onRoutePOISearchDone:(AMapRoutePOISearchRequest *)request response:(AMapRoutePOISearchResponse *)response
{
if (response.pois.count == 0)
{
return;
}
}
- 提示语
AMapInputTipsSearchRequest *tips = [[AMapInputTipsSearchRequest alloc] init];
tips.keywords = key;
tips.city = @"北京";
[self.search AMapInputTipsSearch:tips];
输入提示回调
/* 输入提示回调. */
- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response
{
if (response.count == 0)
{
return;
}
[self.tips setArray:response.tips];
[self.tableView reloadData];
}
根据提示语进行的后续操作(来自官方demo)
相关类 AMapTip 输入提示类
AMapTip *tip = self.tips[indexPath.row];
/* 清除annotations & overlays */
- (void)clear
{
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView removeOverlays:self.mapView.overlays];
}
//1)uid为空,location为空,该提示语为品牌词,可根据该品牌词进行POI关键词搜索。
//2)uid不为空,location为空,为公交线路,根据uid进行公交线路查询。
//3)uid不为空,location也不为空,是一个真实存在的POI,可直接显示在地图上。
- (void)clearAndShowAnnotationWithTip:(AMapTip *)tip
{
/* 清除annotations & overlays */
[self clear];
if (tip.uid != nil && tip.location != nil) /* 可以直接在地图打点 */
{
AMapTipAnnotation *annotation = [[AMapTipAnnotation alloc] initWithMapTip:tip];
[self.mapView addAnnotation:annotation];
[self.mapView setCenterCoordinate:annotation.coordinate];
[self.mapView selectAnnotation:annotation animated:YES];
}
else if (tip.uid != nil && tip.location == nil)/* 公交路线,显示出来*/
{
AMapBusLineIDSearchRequest *request = [[AMapBusLineIDSearchRequest alloc] init];
request.city = @"北京";
request.uid = tip.uid;
request.requireExtension = YES;
[self.search AMapBusLineIDSearch:request];
}
else if(tip.uid == nil && tip.location == nil)/* 品牌名,进行POI关键字搜索 */
{
AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
request.keywords = tip.name;
request.city = @"北京";
request.requireExtension = YES;
[self.search AMapPOIKeywordsSearch:request];
}
}
// 已方法衍生的一些回调和后续操作 看官方文档
- 搜索错误回调
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
NSLog(@"Error: %@ - %@", error, [ErrorInfoUtility errorDescriptionWithCode:error.code]);
}
编码与反编码( AMapSearchAPI *search ,AMapSearchAPI必须全局!!!!)
-地理编码
AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
geo.address = @"环球中心";
// 调用 AMapSearchAPI 的 AMapGeocodeSearch 并发起地理编码。
[self.search AMapGeocodeSearch:geo];
地理编码回调
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
if (response.geocodes.count == 0)
{
return;
}
// NSMutableArray *annotations = [NSMutableArray array];
//
// [response.geocodes enumerateObjectsUsingBlock:^(AMapGeocode *obj, NSUInteger idx, BOOL *stop) {
// GeocodeAnnotation *geocodeAnnotation = [[GeocodeAnnotation alloc] initWithGeocode:obj];
// [annotations addObject:geocodeAnnotation];
// }];
//
// if (annotations.count == 1)
// {
// [self.mapView setCenterCoordinate:[annotations[0] coordinate] animated:YES];
// }
// else
// {
// [self.mapView setVisibleMapRect:[CommonUtility minMapRectForAnnotations:annotations]
// animated:YES];
// }
//
// [self.mapView addAnnotations:annotations];
//
NSLog(@"*******%f*******%f",[annotations[0] coordinate].latitude,[annotations[0] coordinate].longitude);
}
地理反编码
//反编码
AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
regeo.requireExtension = YES;
[self.search AMapReGoecodeSearch:regeo];
地理反编码回调
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil && _isSearchFromDragging == NO)
{
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
ReGeocodeAnnotation *reGeocodeAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate
reGeocode:response.regeocode];
[self.mapView addAnnotation:reGeocodeAnnotation];
[self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];
//返回具体位置
NSString * str =[NSString stringWithFormat:@"%@%@%@%@",reGeocodeAnnotation.reGeocode.addressComponent.province,reGeocodeAnnotation.reGeocode.addressComponent.city,reGeocodeAnnotation.reGeocode.addressComponent.district,reGeocodeAnnotation.reGeocode.addressComponent.towncode];
NSLog(@"======%@",str);
}
else /* from drag search, update address */
{
[self.annotation setAMapReGeocode:response.regeocode];
[self.mapView selectAnnotation:self.annotation animated:YES];
}
}
落叶的位置 谱出一首诗
时间在消逝 我们的故事开始
这是第一次
让我见识爱情 可以慷慨又自私
你是我的关键词
--------林俊杰《关键词》