前言

微信的发送位置功能是一个十分方便的功能,他会定位用户当前所在地点,然后请求用户周边的POI,并且还可以通过拖动地图来获取其他的位置发送给对方,本Demo是结合腾讯地图SDK来实现类似的功能。

使用场景

拖动地图选择地图的中心点,然后请求该点周边的门店信息,可以通过设置搜索分类来指定搜索门店的类型,如:美食、学校等。

准备

核心代码:

1、设置大头针,固定在地图中央,并监听地图移动的时候大头针跟随移动:

- (void)mapViewRegionChange:(QMapView *)mapView {
    // 更新位置
    _annotation.coordinate = mapView.centerCoordinate;
}

2、配置周边检索功能,将检索类型设置为"美食":

- (void)searchCurrentLocationWithKeyword:(NSString *)keyword {
CLLocationCoordinate2D centerCoord = self.mapView.centerCoordinate;

    QMSPoiSearchOption *option = [[QMSPoiSearchOption alloc] init];
    if (keyword.length > 0) {
        option.keyword = keyword;
    }
    option.boundary = [NSString stringWithFormat:@"nearby(%f,%f,2000,1)", centerCoord.latitude, centerCoord.longitude];
    [option setFilter:@"category=美食"];
    [self.mapSearcher searchWithPoiSearchOption:option];
}

3、解析检索结果,移动地图视野,并将结果显示在tableView上:

- (void)searchWithPoiSearchOption:(QMSPoiSearchOption *)poiSearchOption didReceiveResult:(QMSPoiSearchResult *)poiSearchResult {
    NSLog(@"%@", poiSearchResult);

    if (poiSearchResult.count == 0) {
        return;
    }

    // 地图移动到搜索结果的第一个位置
    if (_searchBar.text.length > 0) {
        _selectedIndex = 0;
        QMSPoiData *firstData = poiSearchResult.dataArray[0];
        _annotation.coordinate = firstData.location;
        [self.mapView setCenterCoordinate:firstData.location animated:YES];
    } else {
        _selectedIndex = -1;
    }

    _searchResultArray = poiSearchResult.dataArray;
    [_searchResultTableView reloadData];
}

以上就是核心代码,在Demo中还添加了用于显示地址的TableView以及搜索位置的SearchBar,有兴趣的同学可以在文章最下方进入码云下载完整示例。

示例:搜索西二旗地铁附近的美食

腾讯位置服务仿微信发送位置功能-LMLPHP

感兴趣的同学可以在码云中下载Demo尝试一下。

04-07 11:44