Amap API Tutorial: How to implement map GPS coordinate conversion in php
Introduction:
When developing applications based on Amap, it is often necessary to convert GPS coordinates to maps coordinate of. This tutorial will introduce how to use Amap API and PHP to implement this function. Below is a code example.
include 'amap_api.php';
Please make sure the path to the amap_api.php file is set correctly.
function gps_to_map($longitude, $latitude, $key){ $url = "https://restapi.amap.com/v3/assistant/coordinate/convert?"; $url .= "locations=" . $longitude . "," . $latitude . "&"; $url .= "coordsys=gps&output=json&key=" . $key; $result = file_get_contents($url); $data = json_decode($result, true); if($data['status'] == 1){ $locations = $data['locations']; $locations = explode(",", $locations); $longitude = $locations[0]; $latitude = $locations[1]; } return array("longitude" => $longitude, "latitude" => $latitude); }
This function uses the coordinate conversion API of Amap. The parameters $longitude and $latitude represent the longitude and latitude of the GPS coordinates respectively, and $key is the developer key we obtained in step 1.
$longitude = 121.5244; $latitude = 31.0456; $key = "your_key"; $result = gps_to_map($longitude, $latitude, $key); echo "转换后的经度:" . $result['longitude'] . "<br>"; echo "转换后的纬度:" . $result['latitude'];
Please replace $longitude and $latitude with your own GPS coordinates and $key with the key you obtained in step 1.
By using these code examples, we can easily convert coordinates between GPS coordinates and Amap coordinates, adding more possibilities to our applications. Hope this tutorial helps you!
The above is the detailed content of Amap API tutorial: How to implement map GPS coordinate conversion in php. For more information, please follow other related articles on the PHP Chinese website!