高德地圖API之步行路線


步行路線

引入插件 AMap.Walking

<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });    

        //使用插件
        new AMap.Walking({
            map:map,
            panel:"panel"
        }).search([
            {keyword:"寧波大學",city:"寧波"},
            {keyword:"汽車東站",city:"寧波"}
        ],function(status,data){
            console.log(data);
        });

    </script>    
</body>
</html>

 

 

輸入起點和終點,點擊按鈕規划路線

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
        #search{position: absolute;width:200px;height:100px;top:10px;left:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>
    <div id="search">
        起點<input type="text" id="node1"><br>
        終點<input type="text" id="node2"><br>
        <button id="btn">開始導航</button>
    </div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });   

        // 給起點和終點添加自動補全功能
        new AMap.Autocomplete({
            input:"node1"
        }) 
        new AMap.Autocomplete({
            input:"node2"
        }) 

        btn.onclick=function(){
            //使用插件
            new AMap.Walking({
                map:map,
                panel:"panel"
            }).search([
                {keyword:node1.value,city:"寧波"},
                {keyword:node2.value,city:"寧波"}
            ],function(status,data){
                console.log(data);
            });
        }
        

    </script>    
</body>
</html>

 

 

通過經緯度來進行導航

實現鼠標點擊兩個地址,自動進行導航

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
        #search{position: absolute;width:200px;height:100px;top:10px;left:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });   
        
        var i=0,arr=[];
        map.on("click",function(e){
            i++;
            console.log(i);
            
            if(i%2==1){
                arr=[e.lnglat.R,e.lnglat.Q];
                
            }else{
                //使用插件
                new AMap.Walking({
                    map:map,
                    panel:"panel"
                }).search(new AMap.LngLat(arr[0],arr[1]),new AMap.LngLat(e.lnglat.R,e.lnglat.Q),function(status,data){
                    console.log(data);
                });                
            }
        })
        

    </script>    
</body>
</html>

 

 

常規用法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
        #search{position: absolute;width:200px;height:100px;top:10px;left:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });   
        console.log(map.getCenter().toString());//121.549792,29.868388

        new AMap.Walking({
            map:map,
            panel:"panel"
        }).search([121.549792,29.868388],[121.549792,29.468388],function(status,data){
            console.log(data);
        });  
        

    </script>    
</body>
</html>

 

 

注意:如果是大於兩個定位點,不能直接添加第三個,會報錯

search中傳的數組只能傳2個

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
        #search{position: absolute;width:200px;height:100px;top:10px;left:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });   
        console.log(map.getCenter().toString());//121.549792,29.868388

        new AMap.Walking({
            map:map,
            panel:"panel"
        }).search([121.549792,29.868388],[121.549792,29.468388],[121.549792,29.368388],function(status,data){
            console.log(data);
        });  
        

    </script>    
</body>
</html>

 

 

正確的多點規划做法:(在每個定位點外面用再用數組括號包起來)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>map</title>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ce3b1a3a7e67fc75810ce1ba1f83c01a&plugin=AMap.Walking,AMap.Autocomplete"></script> 
    <style>
        *{margin:0;padding:0;list-style: none;}
        #container {width:100%; height: 100%;top:0;left:0;position: absolute; }  
        #panel{position: fixed;width:280px;top:10px;right:10px;background-color: #fff;}
        #search{position: absolute;width:200px;height:100px;top:10px;left:10px;background-color: #fff;}
    </style>
</head>
<body>
    <div id="container"></div> 
    <div id="panel"></div>

    <script>
        var map=new AMap.Map("container",{
            zoom:11
        });   
        console.log(map.getCenter().toString());//121.549792,29.868388

        new AMap.Walking({
            map:map,
            panel:"panel"
        }).search([[121.549792,29.868388],[121.549792,30.468388],[121.549792,31.368388]],function(status,data){
            //console.log(data);
        });  
        

    </script>    
</body>
</html>

現在無效了,我估計因為版本迭代被取消了吧!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM