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:

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

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:

$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 …
];

4. Generate Heatmap Data

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

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

Add each point to the heatmap using addPoint:

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

Finally, obtain the heatmap image data:

$heatmapData = $heatmap->getHeatMapImage();

5. Display the Heatmap

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

<div id="map"></div>

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

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

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

visualizationheatmapBaidu 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

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.