How to use Amap API in php to obtain surrounding POI information of the current location

王林
Release: 2023-07-30 21:04:02
Original
1807 people have browsed it

How to use the Amap API in PHP to obtain the surrounding POI information of the current location

1. Overview
In modern applications, obtaining the surrounding POI (points of interest) information of the current location is A very common request. Amap provides a powerful API that allows us to easily obtain surrounding POI information through longitude and latitude. This article will introduce how to use the Amap API in PHP to implement this function.

2. Prerequisites

  1. Gaode Map Developer Account: If you don’t have an account, you can register an account and create an application in the Gaode Map Developer Center.
  2. PHP environment: Make sure PHP is installed locally or on the server and can run normally.

3. Obtaining the latitude and longitude
Before starting to use the Amap API, we need to obtain the latitude and longitude of the current location. This can be achieved through the HTML5 Geolocation API provided by the browser. Here is a simple sample code:

<!DOCTYPE html>
<html>
<head>
    <title>获取当前位置</title>
</head>
<body>
    <script>
        // 使用HTML5 Geolocation API获取当前位置的经纬度
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            alert("该浏览器不支持Geolocation API");
        }

        function showPosition(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;

            // 将经纬度传递给后端PHP脚本来获取周边POI信息
            window.location.href = 'get_poi.php?lat=' + lat + '&lng=' + lng;
        }
    </script>
</body>
</html>
Copy after login

In the above code, we use the navigator.geolocation.getCurrentPosition method to get the latitude and longitude of the current location and pass it to get_poi .php file.

4. Obtain surrounding POI information
We create a file named get_poi.php to obtain surrounding POI information from the Amap API. The following is a sample code:

<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];

// 替换为自己的高德地图开发者Key
$key = 'YOUR_AMAP_KEY';

$url = 'https://restapi.amap.com/v3/place/around?key=' . $key . '&location=' . $lng . ',' . $lat . '&output=json&radius=1000&keywords=';

$response = file_get_contents($url);

// 解析JSON响应
$result = json_decode($response, true);

if ($result['status'] == '1') {
    // 获取返回的POI信息
    $pois = $result['pois'];

    // 处理POI信息
    foreach ($pois as $poi) {
        echo $poi['name'] . '<br>';
        echo $poi['address'] . '<br>';
        echo '类型:' . $poi['type'] . '<br>';
        echo '<br>';
    }
} else {
    echo '获取周边POI信息失败';
}
?>
Copy after login

In the above code, we first obtain the latitude and longitude passed from the front end (through the $_GET array), and then construct the URL of the Amap API. Pass the obtained URL to the file_get_contents function to obtain the API response.

The returned response is a JSON-formatted string, which we use the json_decode function to parse into an associative array. We can then get the name, address, and type of each POI by looping through the array.

Note: In the URL in the sample code, we replaced it with our own Amap developer Key. Make sure to replace the Key with your own Key when using the Amap API.

5. Run the sample code
Save the above sample code as a get_poi.php file, and place the file in a server or local environment that can run PHP. Make sure that the HTML file for obtaining the latitude and longitude is placed in the same directory as the get_poi.php file.

Run the HTML file to obtain the latitude and longitude in the browser and allow permission to obtain the current location. The browser will then redirect to the get_poi.php file, passing the latitude and longitude through the URL. The server will call the Amap API based on the latitude and longitude and return surrounding POI information.

6. Summary
Amap API provides a wealth of functions, including obtaining surrounding POI information of the current location. By using PHP and Amap API, we can easily obtain surrounding POI information and use it in the application.

The above is a simple example of using the Amap API in PHP to obtain the surrounding POI information of the current location. Hope this article is helpful to you!

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

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