1、问题背景
百度地图核心js初始化不引入,在函数中创建script后引入
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>
body, html,#allmap {
100%;
height: 100%;
overflow: hidden;
margin:0;
font-family:"微软雅黑";
font-size: 12px;
font-size-adjust: none;
font-style: oblique;
}
</style>
<title>异步加载地图</title>
</head>
<body οnlοad="asyncLoading();">
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
/**
* 异步加载
*/
function asyncLoading()
{
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://api.map.baidu.com/api?v=2.0&ak=ak密钥&callback=initMap";
document.body.appendChild(script);
}
/**
* 初始化地图
*/
function initMap()
{
var map = new BMap.Map("allmap"); // 创建Map实例
map.centerAndZoom(new BMap.Point(114.309531, 30.59619),50);
map.enableScrollWheelZoom(); //启用滚轮放大缩小
}
</script>
3、实现结果