Home >Backend Development >PHP Tutorial >Detailed explanation of how to use Baidu Map API, detailed explanation of how to use api_PHP tutorial
I recently worked on a project, in which there was a requirement to use Baidu Map for navigation. By consulting relevant information An example is completed with reference to Baidu Map API.
Example 1:
API address: http://developer.baidu.com/map/jsdemo.htm#a1_2
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";} </style> <!--调用百度api --> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script> <title>地图展示</title> </head> <body> <div id="allmap"></div> </body> </html> <script type="text/javascript"> // 百度地图API功能 var map = new BMap.Map("allmap"); // 创建Map实例 map.centerAndZoom("西安", 5); // 初始化地图,用城市名设置地图中心点 map.addControl(new BMap.MapTypeControl()); //添加地图类型控件 map.setCurrentCity("深圳"); // 设置地图显示的城市 此项是必须设置的 map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放 var point = new BMap.Point(116.404, 39.915); var marker = new BMap.Marker(point); // 创建点 map.addOverlay(marker); //添加点 map.removeOverlay(marker); //删除点 // 创建地址解析器实例 var myGeo = new BMap.Geocoder(); //批量解析 var adds = ["长沙", "深圳", "香港", "郑州 ", "惠州", "南昌", "赣州", "中山", "阳江", "上海", "无锡", "南京"]; for (var i = 0; i < adds.length; i++) { myGeo.getPoint(adds[i], function (point) { if (point) { var address = new BMap.Point(point.lng, point.lat); var marker = new BMap.Marker(address); map.addOverlay(marker); var opts = { width: 120, // 信息窗口宽度 height: 70, // 信息窗口高度 title: "项目信息" // 信息窗口标题 } var infoWindow = new BMap.InfoWindow("<a href='#' target='blank'>查看详情</a>", opts); // 创建信息窗口对象 marker.addEventListener("click", function () { map.openInfoWindow(infoWindow,address); //开启信息窗口 }); } }, "深圳市"); } getBoundary("中国"); function getBoundary(sRegion) { var bdary = new BMap.Boundary(); bdary.get(sRegion, function (rs) { //获取行政区域 var count = rs.boundaries.length; //行政区域的点有多少个 for (var i = 0; i < count; i++) { var ply = new BMap.Polygon(rs.boundaries[i], { strokeWeight: 2, strokeColor: "#4A7300", fillColor: "#FFF8DC" }); //建立多边形覆盖物 map.addOverlay(ply); //添加覆盖物 } }); } </script>
The effect is as follows:
Example 2:
Baidu Map API is written in JavaScript language. You need to reference the API to the page before using it: Now the new version requires a key, and the old version is used below
7b60c3d2ccd3d8d11a21b923031217402cacc6d41bbb37262a98f745aa00fbf0
Show a simple example of Guangzhou Railway Station:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>百度地图API的使用</title> <!-- 百度地图API--> <script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script> <script type="text/javascript"> function initialize() { //创建地图实例 var map = new BMap.Map('map'); //创建一个坐标 var point =new BMap.Point(113.264641,23.154905); //地图初始化,设置中心点坐标和地图级别 map.centerAndZoom(point,15); } window.onload = initialize; </script> </head> <body> <!-- 百度地图地图容器--> <div id="map" style="width:500px;height:320px"></div> </body> </html>
Add controls on the map:
//Add controls
map.addControl(new BMap.MapTypeControl());
MapTypeControl ---------Map type control
CopyrightControl --------Copyright Control
ScaleControl --------Scale control
NavigationControl ------Zoom control
OverviewMapControl ----Thumbnail control
Create annotation:
var marker = new BMap.Marker(point); // 创建标注 map.addOverlay(marker); // 将标注添加到地图中
Create information window:
var infoWindow = new BMap.InfoWindow("I am here"); // 创建信息窗口对象 map.openInfoWindow(infoWindow,point); //开启信息窗口
Baidu map offset:
Longitude correction value: 0.008774687519;
Latitude correction value: 0.00374531687912;