高德地图创建地图后地图不显示

一、 创建地图

	 根据官方文档提供的方法创建地图, 地图会被挂载到id为 container的div盒子上。
	 运行后地图并没有显示在页面上:
	 一、 检查是否给挂载的盒子添加了宽高, 如果没有设定宽高的话,地图是显示不出来的
	 二、如果设置了宽高, 查看是否在别的地方已经显示了这个地图还没有销毁, 地图只会挂载一次,所以后面你在次调用就是普通的div盒子了, 并没有携带地图样式
     三、 想要调用多个地图, 最好是创建多个地图, 在不在使用的时候,销毁就好了防止内存泄露
     四、 如果地图挂载的盒子宽高是百分比,则要思考其相对定位的对象宽高是否可寻。
// 官方文档提供的一个案例
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>覆盖物的添加与移除</title>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <script src="https://cache.amap.com/lbs/static/es5.min.js"></script>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
    <style>
        html,
        body,
        #container {
          width: 80%;
          height: 80%;
        }
        
        label {
            width: 55px;
            height: 26px;
            line-height: 26px;
            margin-bottom: 0;
        }
        button.btn {
            width: 80px;
        }
      .stop {
  border: 4px solid #2f9cff;
          width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #fff;
  cursor: pointer;
}
    </style>
</head>
<body>
<div id="container"></div>
  <div id="container"></div>
<div class="input-card" style="width:24rem;">
    <h4>添加、删除覆盖物</h4>
    <div class="input-item">
      <label>Marker:</label>
      <button class="btn" id="add-marker" style="margin-right:1rem;">添加Marker</button>
      <button class="btn" id="remove-marker">删除Marker</button>
    </div>
    <div class="input-item">
      <label>Circle:</label>
      <button class="btn" id="add-circle" style="margin-right:1rem;">添加Circle</button>
      <button class="btn" id="remove-circle">删除Circle</button>
    </div>
  </div>

<script>
var map = new AMap.Map('container', {
    resizeEnable: true,
    zoom:11,
    center: [116.397428, 39.90923]
});
    // 构造点标记
var marker = new AMap.Marker({
    icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
  label: {
      content: `<div >dsafadsfasdf</div>`,
      offset: [-5, 14],
    }, 
  content: `<div class='stop'><div/>`,
    position: [116.405467, 39.907761]
});
// 构造矢量圆形
var circle = new AMap.Circle({
    center: new AMap.LngLat("116.403322", "39.920255"), // 圆心位置
    radius: 1000,  //半径
    strokeColor: "#F33",  //线颜色
    strokeOpacity: 1,  //线透明度
    strokeWeight: 3,  //线粗细度
    fillColor: "#ee2200",  //填充颜色
    fillOpacity: 0.35 //填充透明度
});


//事件绑定
document.querySelector("#add-marker").onclick = function() {
    map.add(marker);
    map.setFitView();
}
document.querySelector("#remove-marker").onclick = function() {
    map.remove(marker);
    map.setFitView();
}
document.querySelector("#add-circle").onclick = function() {
    map.add(circle);
    map.setFitView();
}
document.querySelector("#remove-circle").onclick = function() {
    map.remove(circle);
    map.setFitView();
}

</script>
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值