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 prerequisite setup, library inclusion, data preparation, heatmap generation using the HeatMap class, and rendering the map with JavaScript on a web page.

php Courses
php Courses
php Courses
Generating Heatmaps with Baidu Map API in PHP

Heatmaps are visualizations that use color gradients to represent data density. In PHP, the Baidu Map API can be used to generate and display such heatmaps. This article walks through the complete process with code examples.

1. Preparation

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

2. Include Baidu Map API

Download the Baidu Map API library, extract it, and place the folder in an appropriate location within your PHP project (e.g., the vendor directory). Then include the library in your code:

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

3. Prepare Heatmap Data

Heatmap data consists of latitude‑longitude points with associated weight values. Typically you retrieve these from a database or a file and store them in a two‑dimensional array named $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 an instance of the HeatMap class and set basic parameters such as scale and opacity:

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

Add each point from $heatPoints to the heatmap:

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 a Baidu Map, add a heatmap overlay, and feed it 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

Following these steps, you can easily generate and render heatmaps in a PHP project, allowing you to visualize data density with gradient colors for better analysis.

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.

BackendPHPData visualizationGISheatmapBaidu 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

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.