1. 先上程序
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Asynchronous</title> <script type="text/javascript"> function initialize() { var map = new BMap.Map('map'); map.centerAndZoom(new BMap.Point(113.371, 23.133), 16); } function loadScript() { var script = document.createElement("script"); script.src = "http://api.map.baidu.com/api?v=1.2&callback=initialize"; document.body.appendChild(script); } window.onload = loadScript; </script> </head> <body> <div id="map" style="width:500px;height:320px"></div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Asynchronous</title> <script type="text/javascript"> function initialize() { var map = new BMap.Map('map'); map.centerAndZoom(new BMap.Point(113.371, 23.133), 16); } function loadScript() { var script = document.createElement("script"); script.src = "http://api.map.baidu.com/api?v=1.2&callback=initialize"; document.body.appendChild(script); } window.onload = loadScript; </script> </head> <body> <div id="map" style="width:500px;height:320px"></div> </body> </html>
2. 百度地图API
如上程序所示,更简单的加载方式如此:
<script src="http://api.map.baidu.com/api?v=1.2" type="text/javascript"></script>3. BMap类
百度地图API提供了最基础的BMap类,我们可以通过其构造函数创建一个BMap对象。
var map = new BMap.Map('map');4. centerAndZoom
BMap的一个成用方法,就是:
centerAndZoom(BMap.Point point, var zoomRatio);在创建了一个BMap对象后,调用这个方法,相当于完成了对BMap的初始化。示例如开始的程序所示。第一个参数是所显示地图区域的中心点的经纬度,第二个参数是缩放比例,数值越大,表示放大的倍数越大。