Home  >  Article  >  Backend Development  >  How to use the Amap API in php to implement map location search

How to use the Amap API in php to implement map location search

PHPz
PHPzOriginal
2023-07-29 22:19:451793browse

How to use Amap API in PHP to implement map location search

In recent years, with the rapid development of the Internet, map applications have become an indispensable part of people's lives. As China's leading map application, Amap has powerful map functions and rich API interfaces, making it a favorite map choice for many developers. This article will introduce how to use the PHP language and combine it with the Amap API to implement the search function around the location of the map in the web page.

1. Create an AMAP developer account

First, we need to go to the [Amap Developer Platform](https://lbs.amap.com /dev/index), register a developer account, create a new application, and obtain the API key for subsequent use.

2. Introduce Amap API

Introduce the Javascript library file of Amap API at the head of the php file. The specific code is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>地点周边搜索</title>
  <script src="https://webapi.amap.com/maps?v=1.4.15&key=您的API Key"></script>
</head>
<body>
  <div id="map" style="width: 800px; height: 600px;"></div>
</body>
</html>

3. Create a map object

Add the following Javascript code in the php code to create a map object and display the map in the specified container middle.

<script>
  var map = new AMap.Map('map', {
    zoom: 15, // 设置地图缩放级别
    center: [116.397428, 39.90923] // 设置地图中心点坐标
  });
</script>

In the code, we specify a div element with the id map as the container for map display, and set the zoom level and the coordinates of the center point. These parameters can be adjusted according to actual needs.

4. Add the search function around the location

The following is a sample code for implementing the search function around the location on the map.

<script>
  // 创建一个自定义的搜索控件
  var searchMarker = new AMap.Marker({
    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", // 搜索结果标记图标
    position: [116.397, 39.907] // 搜索结果坐标
  });

  // 添加搜索结果标记到地图上
  map.add(searchMarker);

  // 监听点击地图事件,获取点击坐标
  map.on('click', function(e) {
    var lnglat = e.lnglat; // 获取点击坐标
    var keyword = '餐厅'; // 设置搜索关键词
    var radius = 1000; // 设置搜索半径

    // 使用高德地图的PlaceSearch插件进行地点搜索
    var placeSearch = new AMap.PlaceSearch({
      city: '北京', // 指定搜索的城市
      map: map // 将搜索结果显示在地图上
    });

    // 在指定坐标周边搜索相关地点
    placeSearch.searchNearBy(keyword, lnglat, radius, function(status, result) {
      if (status === 'complete') {
        // 循环遍历搜索结果,将结果标记到地图上
        for (var i = 0; i < result.poiList.pois.length; i++) {
          var poi = result.poiList.pois[i];
          var marker = new AMap.Marker({
            position: poi.location,
            title: poi.name
          });
          marker.setMap(map);
        }
      }
    });
  });
</script>

In the code, we first create a custom search control and specify the mark icon and coordinates of the search results. Then, by listening to click events on the map, the click coordinates are obtained, and the PlaceSearch plug-in of Amap is called to conduct a search around the location. Search results are displayed as markers on the map.

It is worth noting that the parameters in the code can be adjusted according to actual needs, such as search keywords, search radius, city, etc.

Through the above steps, we can use the Amap API in the php file to implement the location search function around the map. In this way, users can use the web page we developed to conveniently search for places of interest on the map.

The above is the detailed content of How to use the Amap API in php to implement map location search. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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