Backend Development 5 min read

Generating Baidu Map Heatmaps with PHP

This tutorial explains how to use PHP together with the Baidu Map API to prepare data, create a HeatMap instance, add weighted points, generate the heatmap image, and display it on a web page, providing complete code examples for each step.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Generating Baidu Map Heatmaps with PHP

Heatmaps are visualizations that use color gradients to represent data density. In PHP we can leverage the Baidu Map API to generate and display such heatmaps.

1. Preparation

Before starting, ensure you have a Baidu Map developer account, have created an application, and obtained an API access key from the Baidu Map Open Platform.

2. Include Baidu Map API

Download the Baidu Map API library, unzip it, and place the folder (e.g., under vendor ) in your project. Then require the autoloader and import the HeatMap class:

<code>require 'vendor/bdmapapi-master/autoload.php';
use BaiduMapAPIHeatMapHeatMap;
</code>

3. Prepare Heatmap Data

Collect the data points you want to visualize. Each point should contain longitude ( lng ), latitude ( lat ) and a weight ( count ). Example PHP array:

<code>$heatPoints = [
    ['lng' => 113.943062, 'lat' => 22.549006, 'count' => 10],
    ['lng' => 114.064871, 'lat' => 22.548925, 'count' => 20],
    ['lng' => 113.88908,  'lat' => 22.580623, 'count' => 30],
    // more points …
];
</code>

4. Generate Heatmap Data

Create a HeatMap instance and configure basic parameters such as scale and opacity:

<code>$heatmap = new HeatMap();  // create instance
$heatmap->setScale(3);      // weight scaling factor
$heatmap->setOpacity(0.8);   // heatmap opacity
</code>

Add each point to the heatmap using addPoint :

<code>foreach ($heatPoints as $point) {
    $heatmap->addPoint($point['lng'], $point['lat'], $point['count']);
}
</code>

Finally, obtain the heatmap image data:

<code>$heatmapData = $heatmap->getHeatMapImage();
</code>

5. Display the Heatmap

In your HTML page, create a container for the map:

<code>&lt;div id="map"&gt;&lt;/div&gt;
</code>

Then, using JavaScript, instantiate the Baidu Map, add a heatmap overlay, and feed the generated data:

<code>var map = new BMap.Map("map"); // create map instance
var heatmapOverlay = new BMapLib.HeatmapOverlay(); // create overlay
map.centerAndZoom(new BMap.Point(113.943062, 22.549006), 13); // set center and zoom
map.addOverlay(heatmapOverlay); // add overlay
heatmapOverlay.setDataSet({ data: <?php echo json_encode($heatmapData); ?> }); // set data
</code>

By following these steps you can easily generate and display a Baidu Map heatmap in a PHP project, turning raw coordinate‑weight data into an intuitive visual representation.

Conclusion

This article demonstrated how to combine PHP with the Baidu Map API to create heatmap visualizations, providing complete code snippets for data preparation, heatmap generation, and front‑end display, helping developers quickly add density‑based maps to their applications.

APIvisualizationheatmapBaidu Map
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.