使用场景
出行类APP的起终点位置的选择以及社交类应用的位置分享。
用到产品
核心类/接口
类接口说明版本
MAMapViewsetCenterCoordinate:animated:设置地图中心点。V2.0.0版本起
AMapSearchAPIAMapReGoecodeSearch:逆地址编码查询接口。V3.0.0版本起
AMapPOIAroundSearch:POI周边查询接口。V3.0.0版本起
AMapReGeocodeSearchRequestlocation中心点坐标。V3.0.0版本起
requireExtension是否返回扩展信息,默认NO。V3.0.0版本起
AMapPOIAroundSearchRequestlocation中心点坐标。V3.0.0版本起
radius查询半径,范围:0-50000,单位:米 [default = 3000] 。V3.0.0版本起
AMapPOISearchBaseRequesttypes类型,多个类型用“分割 可选值:文本分类、分类代码 。V3.0.0版本起
requireExtension是否返回扩展信息,默认NO。V3.0.0版本起
AMapSearchDelegateonPOISearchDone:response:POI查询回调函数。V3.0.0版本起
核心难点
1、指定所需类型进行相关 POI 的搜索,这个示例中的类型编码,如下:
住宅区:120300
学校:141200
楼宇:120200
商场:060111
若您的业务需要搜索其他类型的POI,请参考POI分类表。
2、第一次进到页面中,基于定位位置,在定位回调中进行POI搜索和逆地理编码。
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
if(!updatingLocation)
return ;
if (userLocation.location.horizontalAccuracy < 0)
{
return ;
}
// only the first locate used.
if (!self.isLocated)
{
self.isLocated = YES;
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude)];
[self actionSearchAround];
self.mapView.userTrackingMode = MAUserTrackingModeFollow;
}
}
func mapView(_ mapView: MAMapView!, didUpdate userLocation: MAUserLocation!, updatingLocation: Bool) {
if !updatingLocation {
return
}
if userLocation.location.horizontalAccuracy < 0 {
return
}
// only the first locate used.
if !self.isLocated {
self.isLocated = true
self.mapView.userTrackingMode = .follow
self.mapView.centerCoordinate = userLocation.location.coordinate
self.actionSearchAround(at: userLocation.location.coordinate)
}
}
3、拖动地图时,在地图区域改变的回调中进行POI周边搜索。
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if (!self.isMapViewRegionChangedFromTableView && self.mapView.userTrackingMode == MAUserTrackingModeNone)
{
[self actionSearchAround];
}
self.isMapViewRegionChangedFromTableView = NO;
}
func mapView(_ mapView: MAMapView!, regionDidChangeAnimated animated: Bool) {
if !self.isMapViewRegionChangedFromTableView && self.mapView.userTrackingMode == .none {
self.actionSearchAround(at: self.mapView.centerCoordinate)
}
self.isMapViewRegionChangedFromTableView = false
}
4、通过POI搜索回调和逆地理编码的回调获取数据填充到TableView中。
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
if (self.isFromMoreButton == YES)
{
self.isFromMoreButton = NO;
}
else
{
[self.searchPoiArray removeAllObjects];
[self.moreButton setTitle:@"更多.." forState:UIControlStateNormal];
self.moreButton.enabled = YES;
self.moreButton.backgroundColor = [UIColor whiteColor];
}
if (response.pois.count == 0)
{
NSLog(@"没有数据了");
[self.moreButton setTitle:@"没有数据了.." forState:UIControlStateNormal];
self.moreButton.enabled = NO;
self.moreButton.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
self.selectedIndexPath = nil;
[self.tableView reloadData];
return;
}
[response.pois enumerateObjectsUsingBlock:^(AMapPOI *obj, NSUInteger idx, BOOL *stop) {
[self.searchPoiArray addObject:obj];
}];
self.selectedIndexPath = nil;
[self.tableView reloadData];
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
if (response.regeocode != nil)
{
self.currentRedWaterPosition = response.regeocode.formattedAddress;
NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[reloadIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
func onPOISearchDone(_ request: AMapPOISearchBaseRequest!, response: AMapPOISearchResponse!) {
if isFromMoreButton {
isFromMoreButton = false
}
else {
self.searchPoiArray.removeAll()
self.moreButton.setTitle(kMoreButtonTitle, for: UIControlState.normal)
self.moreButton.isEnabled = true
self.moreButton.backgroundColor = UIColor.white
}
if response.count == 0 {
self.moreButton.setTitle("没有数据了...", for: UIControlState.normal)
self.moreButton.isEnabled = false
self.moreButton.backgroundColor = UIColor.gray
self.selectedIndexPath = nil
self.tableView.reloadData()
return
}
self.searchPoiArray.append(contentsOf: response.pois)
self.selectedIndexPath = nil
self.tableView.reloadData()
}
func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
if response.regeocode != nil {
self.currentAddress = response.regeocode.formattedAddress;
self.tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.none)
}
}