请问老师起点怎么设置不了的呢

来源:8-11 自由编程

慕斯卡8252506

2019-09-24 17:48:02

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>导航路线</title>
        <link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
                list-style: none;

            }

            .fa {
                display: inline-block;
                color: gainsboro;
                font-size: 22px;
                margin: 30px 0 10px 30px;
            }

            .fa:hover {
                color: white;
            }

            #container {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;

            }

            .mapNav {
                position: absolute;
                top: 20px;
                left: 10px;
                z-index: 3;
                width: 400px;
                height: 300px;
                background: #6495ED;
                text-align: center;
            }

            .userText {
                width: 200px;
                height: 30px;
                line-height: 30px;
                margin-left: 100px;
                color: white;
                background: royalblue;
                margin-bottom: 10px;
            }

            #origin,
            #terminal {
                margin-left: 5px;
                background: royalblue;
                height: 20px;
                border-style: none;
                color: white;
                font-size: 14px;
            }

            #navWay {
                width: 70px;
                height: 40px;
                background: steelblue;
                border-style: none;
                color: white;
                margin-right: -130px;
            }

            #panel {
                position: fixed;
                background: white;
                width: 400px;
                top: 320px;
                left: 20px;
            }
        </style>
        <script src="js/jQuery-3.4.1.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=    f2a222bf706cb7d0acbdac83cf05c57f&plugin=AMap.Autocomplete,AMap.Driving,AMap.Transfer,AMap.Riding"></script>

    </head>

    <body>
        <div id="container"></div>
        <div id="panel"></div><!-- 放导航盒子 -->
        <!--选择导航  -->
        <div class="mapNav">
            <ul id="navSelect">
                <li class="fa fa-car"></li><!-- 汽车 -->
                <li class="fa fa-bus"></li>
                <!--公交 -->
                <li class="fa fa-bicycle"></li><!-- 骑行 -->
            </ul>

            <div class="userText">
                起<input type="text" id="origin" placeholder="请输入起点.." />
            </div>
            <div class="userText">
                终<input type="text" id="terminal" placeholder="请输入终点.." />
            </div>
            <button id="navWay">开车去</button>
        </div>

        <script type="text/javascript">
            var map = new AMap.Map("container", {
                zoom: 12,

            });
            var $navWay = $("#navWay");
            var $fa = $(".fa");
            $fa.on("click", function() {
                console.log($(this).index())
                if ($(this).index() == 0) {
                    $navWay.html("开车去")
                } else if ($(this).index() == 1) {
                    $navWay.html("公交去")
                } else {
                    $navWay.html("骑行去")
                }
            });

            new AMap.Autocomplete({
                input: "origin"
            })
            new AMap.Autocomplete({
                input: "terminal"
            })

            navWay.onclick = function() {
                if (navWay.innerHTML =="开车去") {

                    new AMap.Driving({
                        map: map,
                        panel: "panel"
                    }).search([{
                            keyword: origin.value,
                            city: "广州"
                        },
                        {
                            keyword: terminal.value,
                            city: "广州"
                        }
                    ], function(status, data) {
                        
                    })

                } else if (navWay.innerHTML == "公交去") {
                    new AMap.Transfer({
                        map: map,
                        panel: panel
                    }).search([{
                            keyword: origin.value,
                            city: "广州"
                        },
                        {
                            keyword: terminal.value,
                            city: "广州"
                        }
                    ], function(){})

                } else {
                    new AMap.Riding({
                        map: map,
                        panel: panel
                    }).search([{
                            keyword: origin.value,
                            city: "广州"
                        },
                        {
                            keyword: terminal.value,
                            city: "广州"
                        }
                    ], function(status, data){})
                }
            }
        </script>

    </body>
</html>

写回答

1回答

好帮手慕夭夭

2019-09-24

你好同学,首先老师使用的你代码测试,搜索是没有任何反应的。这是因为使用的高德地图是最新版本的,需要重新申请key。如果同学那边是可以正常使用的,就不用管了。如果使用不了,请重新申请一个key。

不起作用是因为dom对象没有获取就直接使用了,虽然js中,也可以直接使用元素的id名,但是因为起点的id特殊,js中有一个属性也叫做origin。所以直接使用它不行。建议如下修改更加规范:

http://img.mukewang.com/climg/5d89f0bb0934d41f08040574.jpg

祝学习愉快,望采纳。

0

0 学习 · 6815 问题

查看课程