腾讯位置服务教你轻松搞定微信发送位置功能

如下内容转载自面糊的文章《模仿微信发送位置功能》

做者:面糊git

连接:https://www.jianshu.com/p/47b...微信

来源:简书ide

著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。ui

前言

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

使用场景

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

准备

核心代码:

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

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

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

- (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];
}

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

- (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,有兴趣的同窗能够在文章最下方进入码云下载完整示例。string

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

西二旗.png

连接

感兴趣的同窗能够在码云中下载Demo尝试一下。

相关文章
相关标签/搜索