Current Location: Home> Latest Articles> How to Use Gaode Map API in PHP to Retrieve Surrounding POI Information Based on Current Location

How to Use Gaode Map API in PHP to Retrieve Surrounding POI Information Based on Current Location

M66 2025-06-19

How to Use Gaode Map API in PHP to Retrieve Surrounding POI Information Based on Current Location

1. Overview

In modern applications, retrieving surrounding POI (Point of Interest) information based on the current location is a common requirement. Gaode Map provides a powerful API that makes it easy to get surrounding POI information through latitude and longitude. This article will introduce how to use the Gaode Map API in PHP to implement this feature.

2. Prerequisites

  1. Gaode Map Developer Account: If you don't have one, you can register at the Gaode Map Developer Center and create an application.
  2. PHP Environment: Ensure that PHP is installed and working properly on your local or server environment.

3. Getting Latitude and Longitude

Before using the Gaode Map API, we need to obtain the current latitude and longitude. This can be done using the HTML5 Geolocation API provided by the browser. Below is a simple example:
<!DOCTYPE html>
<html>
<head>
    <title>Get Current Location</title>
</head>
<body>
    <script>
        // Use the HTML5 Geolocation API to get the current latitude and longitude
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            alert("This browser does not support the Geolocation API.");
        }

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

            // Pass the latitude and longitude to the backend PHP script to get the surrounding POI information
            window.location.href = 'get_poi.php?lat=' + lat + '&lng=' + lng;
        }
    </script>
</body>
</html>
In the above code, we use `navigator.geolocation.getCurrentPosition` to get the current latitude and longitude, and pass them to the `get_poi.php` file.

4. Getting Surrounding POI Information

We create a file named `get_poi.php` to fetch surrounding POI information from the Gaode Map API. Below is the example code:
<?php
$lat = $_GET['lat'];
$lng = $_GET['lng'];

// Replace with your own Gaode Map developer 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);

// Parse the JSON response
$result = json_decode($response, true);

if ($result['status'] == '1') {
    // Get the returned POI information
    $pois = $result['pois'];

    // Process POI information
    foreach ($pois as $poi) {
        echo $poi['name'] . '<br>';
        echo $poi['address'] . '<br>';
        echo 'Type: ' . $poi['type'] . '<br>';
        echo '<br>';
    }
} else {
    echo 'Failed to retrieve surrounding POI information.';
}
?>

In this code, we first retrieve the latitude and longitude from the front-end via the $_GET array. Then, we build the Gaode Map API URL and use the file_get_contents function to fetch the API response.

The response is a JSON-formatted string, and we use the json_decode function to parse it into an associative array. We can then loop through the array to retrieve each POI's name, address, and type.

Note: Be sure to replace YOUR_AMAP_KEY with your own Gaode Map developer Key.

5. Running the Example Code

Save the above code as the `get_poi.php` file and place it on a PHP-enabled server or local environment. Ensure that the HTML file used to fetch the latitude and longitude and the `get_poi.php` file are in the same directory.

Open the HTML file in a browser, allow the browser to access the location, and it will redirect to the get_poi.php file, passing the latitude and longitude via the URL. The server will then call the Gaode Map API based on the latitude and longitude and return the surrounding POI information.

6. Conclusion

Gaode Map API provides rich functionality, including retrieving surrounding POI information based on the current location. By using PHP in combination with the Gaode Map API, we can easily retrieve surrounding POI information and integrate it into applications.

This article provides a simple example to help developers integrate the Gaode Map API in PHP projects and retrieve surrounding POI information. I hope this article is helpful to you!