话不多说,直接给兄弟们上干货,修改了好多次,之前都是获取到路线但是无法删除,这次是根据点击的单双判断开始和结束
<!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><script src="./js/jquery-3.7.1.js"></script><style type="text/css">html,body,#container {width: 100%;height: 100%;}#container {width: 80%;margin-left: 10%;height: 600px;}#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;}#qd {width: 150px;height: 40px;position: fixed;top: 260px;background-color: gray;color: white;font-size: 10px;}#zd {width: 150px;height: 40px;position: fixed;top: 300px;background-color: gray;color: white;font-size: 10px;}</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><script type="text/javascript">window._AMapSecurityConfig = {securityJsCode: "13f25737b187aa80ea65cdc53509e34d",};</script><body><div id="container"></div><div id="panel"></div><div id="qd"></div><div id="zd"></div><script type="text/javascript"src="https://webapi.amap.com/maps?v=2.0&key=effbe0fe2503f592b2652c1874584243&plugin=AMap.Driving"></script><script type="text/javascript">//基本地图加载var map = new AMap.Map("container", {resizeEnable: true,zoom: 13 //地图显示的缩放级别});//构造路线导航类var driving = new AMap.Driving({map: map,panel: "panel"});var jd;var wd;var num = 0;map.on('click', function(e) {num++;console.log(e.lnglat.getLng(), e.lnglat.getLat());if (num % 2 == 1) {jd = new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat());$('#qd').html('');$('#qd').html(function(index, oldHtml) {return oldHtml + '起经:' + e.lnglat.getLng() + '   ' + '起纬:' + e.lnglat.getLat();});} else if (num % 2 == 0) {wd = new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat());$('#zd').html('');$('#zd').html(function(index, oldHtml) {return oldHtml + '起经:' + e.lnglat.getLng() + '   ' + '起纬:' + e.lnglat.getLat();});}// 根据起终点经纬度规划驾车导航路线driving.search(jd, wd),function(status, result) {if (status === 'complete') {log.success('绘制驾车路线完成')map.setCenter([jd, wd])} else {log.error('获取驾车数据失败:' + result)}};});</script></body>
</html>
兄弟们点下赞,搞了好长时间,碰到了好多壁,希望大家关注下
我是小辉,请大家多多关照