老师,有三点问题麻烦讲解下,谢谢

来源:4-2 地图搜索(2)

qq_Mr郭生_0

2020-07-28 22:13:13

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=faf7b1a7905b56e0468f876e92b57f21"></script>

    <style>

        *{margin: 0;padding: 0;list-style: none;}

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

        #setCenterNode{width: 400px;height: 500px;position: absolute;z-index: 99;right: 20px;top: 20px; border: 1px solid black;box-shadow: 0 0 5px black;background-color: white;}

        #node li{cursor: pointer;}

    </style>

</head>

<body>

    <div id="container"></div>

    <div id="setCenterNode">

        <input type="text" id="searchText">

        <ul id="node"></ul>

    </div>

    <script type="text/javascript">

        var map=new AMap.Map('container',{

            zoom:11,

            center:[100.379391,30.861536]

        });

        //加载插件,地图加载完毕会触发回调函数

        AMap.plugin('AMap.Autocomplete',function(){

            //console.log(1)

            node.innerHTML='';

            searchText.oninput=function(){

                if(this.value==''){

                        return;

                }

                new AMap.Autocomplete().search(this.value,function(status,data){

                    for(var i=0;i < data.tips.length;i++){

                        var oLi=document.createElement('li');

                        oLi.innerHTML=data.tips[i].name;

                        oLi.R=data.tips[i].location.R;

                        oLi.Q=data.tips[i].location.Q;

                        node.appendChild(oLi);

                        oLi.onclick=function(){

                            if(this.Q&&this.R){

                                map.setCenter([this.Q,this.R]);

                            }

                            //console.log(this.Q,this.R)

                            map.setCenter([this.Q,this.R]);

                        }

                    }

                });  

            }

        });

    </script>

</body>

</html>

  1. 无法定位到输入的城市,而且选择某些城市还会报错,如输入桂林市并选择桂林市

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

  2.  输入的字符如果是如‘吧’会报错

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

3.文字还停留在输入法中,还没到输入框就已经开始匹配了这里不对,麻烦老师说下

写回答

1回答

好帮手慕久久

2020-07-29

同学你好,问题解答如下:

1. 输入内容时,搜索结果的html结构会进行累计,如下:

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

所以需要在oninput事件,将之前的html结构先清空,如下:

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

setCenter方法中,由于参数R和Q的位置写反了,所以设置中心点无效。应该时R在前,Q在后,如下:

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

这样点击li,才会改变中心点,如下:

http://img.mukewang.com/climg/5f20eb6a09dcbb1212650237.jpghttp://img.mukewang.com/climg/5f20eb7009bcc4a710630583.jpg

由于默认是在全国范围内搜索,所以点击上面的“桂林路”,会将中心点设置到它对应的城市“长春”。

2. 点击“桂林市”报错,是因为它的数据中,“location”为空,所以它的html结构上,没

有R、Q属性,所以导致setCenter的参数不正确,从而报错,如下:

http://img.mukewang.com/climg/5f20eb8d09335ddc09080624.jpghttp://img.mukewang.com/climg/5f20eb9109920fb714280365.jpg

所以上面多余的setCenter方法要删除,如下:

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

3. 当输入“吧”时,报错信息是“不能读取R,从undefined中”:

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

这是因为该条数据中,“location”是undefined,如下:

http://img.mukewang.com/climg/5f20ebaf094d28d405540143.jpghttp://img.mukewang.com/climg/5f20ebb2095f99ca09890381.jpg

所以这是数据的问题,可做如下处理:

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

4. 当中文状态下输入时,即使文字还处在输入中,但是input中已经是当前输入的内容了,所以会触发oninput事件,如下:

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

这是正常的现象。

可以改成onchange事件,当input失去焦点时,再触发事件,如下:

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

如果我的回答帮到了你,欢迎采纳,祝学习愉快!

0

0 学习 · 6815 问题

查看课程