<!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.
<?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.
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.
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!