1. 程式人生 > 實用技巧 >騰訊位置服務仿微信傳送位置功能

騰訊位置服務仿微信傳送位置功能

以下內容轉載自麵糊的文章《模仿微信傳送位置功能》

作者:麵糊

連結:https://www.jianshu.com/p/47b3ada2e36d

來源:簡書

著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。

前言

微信的傳送位置功能是一個十分方便的功能,他會定位使用者當前所在地點,然後請求使用者周邊的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,有興趣的同學可以在文章最下方進入碼雲下載完整示例。

示例:搜尋西二旗地鐵附近的美食

連結

感興趣的同學可以在碼雲中下載Demo嘗試一下。