Home > Web Front-end > JS Tutorial > Using JavaScript and Tencent Maps to implement map POI search function

Using JavaScript and Tencent Maps to implement map POI search function

WBOY
Release: 2023-11-21 11:13:41
Original
1380 people have browsed it

Using JavaScript and Tencent Maps to implement map POI search function

Use JavaScript and Tencent Maps to implement map POI search function

With the development of mobile Internet, map applications have become more and more indispensable in people's lives. The map POI (Points of Interest) search function can help users quickly find places of interest on the map, such as restaurants, hotels, shopping malls, etc. This article will introduce how to use JavaScript and Tencent Map API to implement the map POI search function, and provide specific code examples.

Tencent Maps is a powerful and widely used map application with detailed and comprehensive geographical information data and rich map functions. To use the Tencent Map API, first we need to register a developer account on the Tencent Map Open Platform and create a new application. Tencent Maps provides JavaScript API interfaces, which can be used to implement various map functions.

The following are the specific steps and code examples to implement the map POI search function:

  1. Add a map container to the HTML page:
<div id="map"></div>
Copy after login
  1. Introduce the script file of Tencent Map API in JavaScript:
<script src="https://map.qq.com/api/js?v=2.exp"></script>
Copy after login
  1. Initialize the map:
var map = new qq.maps.Map(document.getElementById('map'), {
  center: new qq.maps.LatLng(39.916527, 116.397128), // 设置地图中心点坐标
  zoom: 13 // 设置地图缩放级别
});
Copy after login
  1. Add POI search input box:
var searchInput = document.createElement('input');
searchInput.type = 'text';
document.body.appendChild(searchInput);
Copy after login
  1. Listen to the input event of the search box and call the POI search function in real time:
var searchService = new qq.maps.SearchService();
searchInput.addEventListener('input', function(e) {
  var keyword = e.target.value;
  searchService.setComplete(function(results) {
    map.clearOverlays(); // 清除之前的搜索结果
    var poiList = results.detail.pois;
    for (var i = 0; i < poiList.length; i++) {
      var poi = poiList[i];
      var marker = new qq.maps.Marker({
        position: poi.latLng, // 设置标记点位置
        map: map // 设置标记点所属的地图
      });
      var infoWindow = new qq.maps.InfoWindow({
        map: map
      });
      qq.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(poi.name);
        infoWindow.open();
      });
    }
  });
  searchService.search(keyword);
});
Copy after login

In the above code example, a map container div is first created and in JavaScript A qq.maps.Map object is instantiated for displaying the map. Then an input box is added for entering search keywords. By monitoring the input events of the input box, you can obtain the keywords entered by the user in real time and call the POI search service for search. After the search results are returned, the previous search results are cleared and the new search results are displayed on the map. When you click on a marker point in the search results, an information window will pop up to display the name of the POI.

It should be noted that in order to use the Tencent Map API correctly, we need to apply for a key on the Tencent Map Open Platform and add the key to the URL of the script file. For example, modify the script introduction part in the above code as follows:

<script src="https://map.qq.com/api/js?v=2.exp&key=YOUR_API_KEY"></script>
Copy after login

Among them, YOUR_API_KEY needs to be replaced with the Tencent Map API key you applied for.

Through the above steps, we can easily use JavaScript and Tencent Map API to implement the map POI search function. Based on actual needs, we can further customize the display style and interactive behavior of search results to provide a better user experience. At the same time, Tencent Map API also provides other rich functions, such as route planning, geocoding, etc., which developers can expand and learn in depth according to specific needs.

The above is the detailed content of Using JavaScript and Tencent Maps to implement map POI search function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template