Backend Development 5 min read

Generating Heatmaps with Baidu Map API in PHP

This article explains how to create and display heatmaps in a PHP project by integrating the Baidu Map API, covering preparation, library inclusion, data preparation, heatmap generation, and rendering the map with JavaScript, accompanied by complete code examples.

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

Heatmap is a visualization that uses color gradients to show data density. This guide shows how to generate and display heatmaps in PHP using the Baidu Map API.

1. Preparation

Obtain a Baidu Map developer account, create an application, and get an API key.

2. Include Baidu Map API

Download the Baidu Map API library, place it in the project (e.g., under vendor), and include it in PHP code.

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

3. Prepare heatmap data

Collect latitude, longitude, and weight values, store them in an array $heatPoints.

$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, set scale and opacity, then add each point with addPoint(), and finally obtain the heatmap image data.

$heatmap = new HeatMap();  // create instance
$heatmap->setScale(3);      // weight scaling
$heatmap->setOpacity(0.8); // transparency
foreach ($heatPoints as $point) {
    $heatmap->addPoint($point['lng'], $point['lat'], $point['count']);
}
$heatmapData = $heatmap->getHeatMapImage();

5. Display the heatmap

In the HTML page, add a map container <div id="map"></div> , load the Baidu Map JavaScript library, create a map instance, add a HeatmapOverlay, and set its data set using the PHP‑generated $heatmapData.

var map = new BMap.Map("map"); // create map
var heatmapOverlay = new BMapLib.HeatmapOverlay(); // overlay
map.centerAndZoom(new BMap.Point(113.943062, 22.549006), 13);
map.addOverlay(heatmapOverlay);
heatmapOverlay.setDataSet({ data:
});

Following these steps, you can easily generate and render a heatmap in a PHP project, visualizing data density with gradient colors for better analysis.

Backend DevelopmentPHPData VisualizationheatmapBaidu Map API
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.