高德地图API路径规划结果清除

关于高德地图API之路径规划结果的清除(js API)
偶然用到高德地图API做路径规划,在做的过程中发现连续进行不同的路径规划产生的结果是叠加的。如何清除上一次的路径规划呢?开发手册上有一个 clear() 函数,就是用来清除搜索结果的。废话不多说,直接上代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>
    <style type="text/css">
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <style type="text/css">
        #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-transfer {
            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://webapi.amap.com/maps?v=1.4.15&key=关注后端码匠&plugin=AMap.Transfer"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div class="" style='width:18rem;margin-top:50px;margin-bottom: 30px'>
    <label style='color:grey'>忻府区公交线路查询</label>
    <div class="">
        <div class="input-item-prepend"><span class="input-item-text">起始</span></div>
        <input id='starname' type="text" value="市*">
        <div class="input-item-prepend"><span class="input-item-text">终点</span></div>
        <input id='endname' type="text" value="客运站">
    </div>
    <input id="search" type="button" class="btn" value="查询"/>
</div>
<div id="container"></div>
<br>
<div id="panel" style="margin-top:120px;"></div>


<script type="text/javascript">
    var map = new AMap.Map("container", {
        resizeEnable: true,
        center: [112.734112, 38.417743],
        zoom: 13
    });
    var transOptions = {
        map: map,
        city: '忻州市',
        panel: 'panel',
        policy: AMap.TransferPolicy.LEAST_TIME //乘车策略
    };

    var transfer
    /*公交线路查询*/
    function lineSearch() {
        if(transfer)
        {
            //调用clear()函数清除上一次结果,可以清除地图上绘制的路线以及路径文本结果
            transfer.clear();
        }
        //构造公交换乘类
        transfer = new AMap.Transfer(transOptions);


        var start = document.getElementById('starname').value;
        var end = document.getElementById('endname').value;

        //根据起、终点名称查询公交换乘路线
        transfer.search([
            {keyword: start, city: '忻州市'},
            //第一个元素city缺省时取transOptions的city属性
            {keyword: end, city: '忻州市'}
            //第二个元素city缺省时取transOptions的cityd属性
        ], function (status, result) {
            // result即是对应的公交路线数据信息,相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_TransferResult
            if (status === 'complete') {
                log.success('绘制公交路线完成')
            } else {
                log.error('公交路线数据查询失败' + result)
            }
        });

    }

    lineSearch();
    document.getElementById('search').onclick = lineSearch;
</script>
</body>
</html>

☺️

上一篇:聊天室图形界面


下一篇:2021/10/14 智能家具 嵌入式实训 第四天 RGB彩灯(3)