Backend Development 7 min read

How to Build a Map Application with PHP and AMap (Gaode) API

This article guides developers through obtaining an AMap API key, setting up a PHP environment, retrieving geolocation data via the API, and embedding an interactive map with optional routing features using JavaScript, providing complete code examples for each step.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Build a Map Application with PHP and AMap (Gaode) API

How to Use PHP and AMap (Gaode) Map API to Create a Map Application

Map applications are essential in modern society, and using the AMap API is a common way to build them. This guide explains the process using PHP and the AMap API.

1. Apply for an AMap API key

Visit the AMap developer website, register an account, provide valid enterprise information, and obtain an API key after verification.

2. Set up the PHP environment and libraries

Ensure PHP is installed and runnable from the terminal, then install the AMap API library as described in the official documentation.

3. Retrieve geolocation using the AMap API

Use the geocoding service to convert an address to coordinates. Example code:

<code><?php
     $url = 'https://restapi.amap.com/v3/geocode/geo?key=[Your API Key]&address=广州市番禺区广州大道中';
     $data = file_get_contents($url);
     $result = json_decode($data, true);
     $location = $result['geocodes'][0]['location'];
?></code>

The script queries the address, receives JSON data, and stores the latitude/longitude string in $location for later use.

4. Create the map with the AMap API

After obtaining the location, embed an interactive map using the JavaScript library. Example:

<code><![CDATA[<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AMap Example</title>
    <style type="text/css">
       #container{
           width: 100%;
           height: 500px;
       }
    </style>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=[Your API Key]"></script>
    <script type="text/javascript">
        var map = new AMap.Map('container',{
            center: [<?php echo $location; ?>],
            zoom: 10
        });
        var marker = new AMap.Marker({
            position: [<?php echo $location; ?>],
            title: 'My Location',
            map: map
        });
    </script>
</head>
<body>
    <div id="container"></div>
</body>
</html>]]></code>

This HTML creates a div for the map, initializes the map centered on $location , adds a marker, and displays it.

5. Advanced AMap features (routing)

The API also supports driving route planning. Example code:

<code><?php
    $url = 'https://restapi.amap.com/v3/direction/driving?key=[Your API Key]&origin=113.324694,23.099713&destination=113.107257,23.045315';
    $data = file_get_contents($url);
    $result = json_decode($data, true);
    $paths = $result['route']['paths'][0]['steps'];
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AMap Driving Route Example</title>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=[Your API Key]"></script>
    <script type="text/javascript">
        var map = new AMap.Map('container',{
            center: [113.339908,23.146273],
            zoom: 11
        });
        var path = [
            <?php foreach($paths as $step): ?>
            {type: "LineString", coordinates: <?php echo $step['polyline']; ?>},
            <?php endforeach; ?>
        ];
        var polyline = new AMap.Polyline({
            path: path,
            strokeColor: "#00b0ff",
            strokeOpacity: 1,
            strokeWeight: 3,
            strokeStyle: "solid"
        });
        polyline.setMap(map);
    </script>
</head>
<body>
    <div id="container"></div>
</body>
</html></code>

The script fetches a driving route between two points, extracts the steps, and draws the route on the map using a Polyline object.

Conclusion

By following these steps, developers can obtain geolocation data, embed interactive maps, and leverage advanced features such as routing using PHP and the AMap API, facilitating rapid development of map‑based applications.

routingphpgeocodingAmap APIMap Development
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.