Home > Article > Backend Development > How to use Baidu Map API to add and delete map overlays in PHP
How to use Baidu Map API to add and delete map overlays in PHP
Map overlays are various logos, graphics or text elements added to the map to mark specific areas on the map. location or display specific information. Baidu Map API provides rich interfaces and functions, making it very convenient to add and delete map overlays in PHP. This article will introduce how to use Baidu Map API to add and delete map overlays, and provide corresponding code examples.
First, you need to introduce the JavaScript code of Baidu Map API into the PHP file. It can be introduced in the following way:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>地图覆盖物示例</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script> </head> <body> <div id="map" style="width: 100%; height: 500px;"></div> <script type="text/javascript"> // JavaScript代码 </script> </body> </html>
where,http://api.map.baidu.com/api?v=2.0&ak=
ak in your key
is the key you applied for on the Baidu Map Open Platform. Make sure you have applied for and have a valid key.
In the JavaScript code, you first need to add a map container, specify the width and height, and obtain the map instance object, as shown below:
var map = new BMap.Map("map"); // 创建地图实例 map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); // 初始化地图,设置中心点和地图缩放级别
Here"map"
is the container id of the map, you can specify it yourself.
In JavaScript code, create marking points through the BMap.Marker
class and call map.addOverlay
The method adds the label point to the map, as shown below:
var point = new BMap.Point(116.404, 39.915); var marker = new BMap.Marker(point); // 创建标注点 map.addOverlay(marker); // 添加标注点到地图中
The (116.404, 39.915)
here is the longitude and latitude of the label point, you can set it according to your needs.
In addition to marking points, Baidu Map API also supports adding other types of overlays, including circles, polygons, polylines, etc. Take adding a circle as an example:
var circle = new BMap.Circle(point, 1000, {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5}); // 创建圆形,参数分别为圆心、半径和圆的样式 map.addOverlay(circle); // 添加圆形到地图中
Here point
is the latitude and longitude of the center of the circle, 1000
is the radius of the circle, {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5}
is the circular style.
To remove an overlay on the map, simply call the remove
method of the corresponding overlay object, as shown below:
map.removeOverlay(marker); // 删除标注点 map.removeOverlay(circle); // 删除圆形
In this way, you can add and delete overlays on the map.
In summary, through the above steps, we can use Baidu Map API to add and delete map overlays in PHP. We hope that the code examples provided in this article can help readers better understand and apply Baidu Map API.
The above is the detailed content of How to use Baidu Map API to add and delete map overlays in PHP. For more information, please follow other related articles on the PHP Chinese website!