Js地图路线规划以及点击获取经纬度

废话不多说,直接上代码。

代码script引用链接链接来自高德地图开放平台。(高德开放平台 | 高德地图API (amap.com)

<!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">
  <script type="text/javascript">
  	window._AMapSecurityConfig = {
  		securityJsCode: "你的安全密钥",
  	};
  </script>
  <title>位置经纬度 + 驾车规划路线</title>
  <style type="text/css">
    html,
    body,
    #container {
      width: 92%;
      height: 100%;
    }

    #panel {
      position: fixed;
      background-color: white;
      max-height: 90%;
      overflow-y: auto;
      top: 10px;
      right: 10px;
      width: 280px;
    }

    #panel .amap-call {
      background-color: #009cf9;
      border-top-left-radius: 4px;
      border-top-right-radius: 4px;
    }

    #panel .amap-lib-driving {
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
      overflow: hidden;
    }
  </style>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>

  <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>

<body>
  <div id="container"></div>
  <div id="panel"></div>
  <div class="input-card">
  	<h4 style="line-height: 16px;margin: 10px;">左击获取经纬度:</h4>
  	<div class="input-item">
  		<input type="text" readonly="true" id="lnglat">
  	</div>
  </div>
  <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=你的key值&plugin=AMap.Driving"></script>
  <script type="text/javascript">
    //基本地图加载
    var map = new AMap.Map("container", {
      resizeEnable: true,
      center: [116.397428, 39.90923],//地图中心点
      zoom: 13 //地图显示的缩放级别
    });
    //构造路线导航类
    var driving = new AMap.Driving({
      map: map,
      panel: "panel"
    });
    // 根据起终点经纬度规划驾车导航路线
    driving.search(new AMap.LngLat(116.379028, 39.865042), new AMap.LngLat(116.427281, 39.903719), function (status, result) {
      // result 即是对应的驾车导航信息,相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
      if (status === 'complete') {
        log.success('绘制驾车路线完成')
        map.setCenter([116.442581,39.882498])
      } else {
        log.error('获取驾车数据失败:' + result)
      }
    });
	//为地图注册click事件获取鼠标点击出的经纬度坐标
	map.on('click', function(e) {
		document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat()
	});
  </script>
</body>

</html>

感谢观看!!!

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值