The following content is reprinted from the article “Imitating wechat’s Sending Location Function” by Mianbatter.

Akik: Batter

Link: www.jianshu.com/p/47b3ada2e…

Source: Jane Book

Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

preface

Wechat’s sending location function is a very convenient function, it will locate the user’s current location, and then request the POI around the user, and can also drag the map to get other locations to send to the other party, this Demo is combined with Tencent map SDK to achieve similar functions.

Usage scenarios

Drag the map to select the center point of the map, and then request information about stores around the point. You can specify the type of stores to be searched by setting search categories, such as food and school.

To prepare

  • Tencent MAP 3D SDK
  • Marker
  • Site to retrieve

Core code:

1. Set a pin, fix it in the center of the map, and monitor the movement of the pin when the map moves:

- (void)mapViewRegionChange:(QMapView *)mapView {// update location _annotation. Coordinate = mapview.centercoordinate; }Copy the code

2. Configure the peripheral search function and set the search type to “delicious food” :

- (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= delicious "]; [self.mapSearcher searchWithPoiSearchOption:option]; }Copy the code

3. Parse the retrieval result, move the map view, and display the result in 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]; }Copy the code

The above is the core code, in the Demo also added for display address TableView and SearchBar search location, interested students can enter the code cloud at the bottom of the article to download the complete example.

Example: Search for delicious food near xi Erqi subway

link

Interested students can download the Demo in the code cloud to try it out.