高德地图 JS API (地图/生命周期)

写在前面:

开发工具使用HBuilder,只需要将key替换成您申请的key值,代码即可直接使用。

1,地图的创建

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--4.	进行移动端开发时,请在head内添加viewport设置,以达到最佳的绘制性能;-->
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <title>地图显示</title>
    <!--3.	为地图容器指定高度、宽度;-->
    <style>
        html,
        body,
        #container {
          width: 100%;
          height: 100%;
        }
    </style>
</head>
<body>
	<!-- 2.	添加div标签作为地图容器,同时为该div指定id属性; -->
	<div id="container"></div>
	<!-- 1.	加载地图JSAPI脚本,在页面添加 JS API 的入口脚本标签 -->
	<script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
	<script>
		/*
		 * 简单创建一个地图只需要一行代码,构造参数中的container为准备阶段添加的地图容器的id:
		 * 创建的同时可以给地图设置中心点、级别、显示模式、自定义样式等属性:
		 * */
	    var map = new AMap.Map('container', {
	        resizeEnable: true, //是否监控地图容器尺寸变化
	        zoom:11 //初始化地图层级
			,center: [116.397428, 39.90923] //初始化地图中心点''
			,viewMode:'3D'//使用3D视图
	    });
	</script>
</body>
</html>

2,地图的同步加载

<!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">
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <style>
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }
  </style>
  <title>地图加载完成</title>
</head>

<body>
  <div id="container"></div>
  <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
  <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  <script>
    var map = map = new AMap.Map('container', {
        resizeEnable: true
    });
    
    map.on("complete", function(){
       log.success("地图加载完成!");  
    });
  </script>
</body>

</html>

3,地图的异步加载1

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style type="text/css">
      body,html,#container{
        width: 100%;
        height: 100%;
        margin: 0px
      }
    </style>
    <title>API加载</title>
  </head>
  <body>
    <div id="container" tabindex="0"></div>
    <!--
    	作者:2317925373@qq.com
    	时间:2019-07-24
    	描述:异步加载指的是为 JS API 指定加载的回调函数,在 JS API 的主体资源加载完毕之后,将自动调用该回调函数。
    		  回调函数应该声明在 JS API 入口文件引用之前,异步加载可以减少对其他脚本执行的阻塞,HTTPS 下我们也建议使用异步方式:
    -->
    <script type="text/javascript">
        window.init = function(){
            var map = new AMap.Map('container', {
            	resizeEnable: true,
            	center:[116.480983, 39.989628],
            	zoom:11
            });
            /*
             * 访问路径中是否有guide,来展示放大缩小按钮AMap.ToolBar
             * http://127.0.0.1:8020/gaoDeMap/map-shengmingzhouqi-load-yibu.html?guide=1
             * */
            if (location.href.indexOf('guide=1') !== -1) {
                map.setStatus({scrollWheel: false});
                map.plugin(["AMap.ToolBar"], function() {
                	map.addControl(new AMap.ToolBar({liteStyle:true}))
                });
            }
          }
    </script>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&callback=init"></script>
  </body>
</html>

4,地图的创建和销毁

<!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">
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <style>
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }
  </style>
  <title>地图创建与销毁</title>
</head>

<body>
  <div id="container"></div>
  <div class="input-card" style="width:16rem">
    <h4>创建、销毁地图</h4>
    <div id="btns">
      <div class="input-item">
        <button id="create-btn" class="btn" style="margin-right:1rem;">创建地图</button>
        <button id="destroy-btn" class="btn">销毁地图</button>
      </div>
    </div>
  </div>
  <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
  <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  <script>
    var map = null;

    function createMap() {
      map = new AMap.Map('container', {
        resizeEnable: true
      });
      log.success("创建地图成功");
    }

    function destroyMap() {
      map && map.destroy();
      log.info("地图已销毁");
    }

    //初始化地图
    createMap();

    //绑定创建、销毁事件
    document.querySelector("#create-btn").onclick = createMap;
    document.querySelector("#destroy-btn").onclick = destroyMap;
  </script>
</body>

</html>

5,地图的异步加载2

<!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://cache.amap.com/lbs/static/main1119.css"/>
</head>
<body>
<div id="container"></div>
<script>
    function onApiLoaded(){
        var map = new AMap.Map('container', {
//          center: [117.000923, 36.675807],
            zoom: 6
        });
        map.plugin(["AMap.ToolBar"], function() {
            map.addControl(new AMap.ToolBar());
        });
    }
     var url = 'https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&callback=onApiLoaded';
    var jsapi = document.createElement('script');
    jsapi.charset = 'utf-8';
    jsapi.src = url;
    document.head.appendChild(jsapi);
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值