高德地图实现地址检索获取结果列表和坐标

摘要:
--A望京地址:地铁14号线;地铁15号线˂div&g

高德开放平台http://lbs.amap.com/api/javascript-api/summary/

之前使用百度地图API实现地址检索功能,获取检索结果并显示到我们自定义的列表中http://www.cnblogs.com/fozero/p/5924210.html

这里我们换成高德地图同样实现跟之前一样的功能,高德跟百度地图API还是有很大的区别的,左边的标注点这里跟百度地图的不同,我是手动在地图上面对坐标点标注 ,并设置它的文本和信息窗口对应右边的结果列表数据,我们直接看代码:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <style type="text/css">
        body, html{width: 100%;height: 100%; font-family:"微软雅黑";font-size: 14px;}
        *{ margin: 0;padding: 0;}
        .left{float:left;}
        .right{float:right;}
        .clearfix{clear: both;}
        .hide{display: none;}
        #map{height:628px;width: calc(100% - 305px);border: 1px solid #dadada;}
        /*地图标注文本样式*/
        .amap-marker-label{
            background:none;
            border: none;
            color: #fff;
        }
        #result{height: 520px; font-size: 13px; line-height: 20px;overflow: auto;color: #666;}
        #result ul {
            list-style: outside none none;
        }
        #result ul li{
            border-bottom: 1px solid #dadada;
            padding: 10px;
        }
        #result ul li:hover{
            background-color: #f0f0f0;
            cursor: pointer;
        }
        #result .res-data{
            background: url(img/ico_marker.png) no-repeat -1px 15px;
        }
        #result .res-marker{
            width: 30px; 
            height: 58px; 
            line-height: 58px; 
            text-align: center; 
            color: rgb(255, 255, 255); 
            font-weight: bold;
        }
        #result .res-address{
            width: 235px;
        }
        #result .title{
            font-size: 16px;
            color: #000000;
        }
        .area-right{
            width:303px;
        }
        .area-right .search{
             border-bottom: 1px solid #dadada;
            padding: 8px 0;
            box-shadow: 5px 2px 5px #888888;
            margin-bottom: 8px;
        }
        .area-right .search .s-address{
            margin-bottom: 5px;position: relative;border-bottom: 1px solid #DADADA;padding: 0 10px;height: 32px;line-height: 32px;
        }
        .area-right .search .s-address .btn{
            position: absolute;right: 10px;top: 5px;cursor: pointer;
        }
        .area-right .search .s-address .btn img{
            width: 22px;
        }
        .area-right .search .address{
            height: 28px;
            line-height: 28px;
            border: none;
            outline: medium;/*去掉鼠标点击后的边框*/
        }
        
        .area-right .search .cur_point{
            color: #1E90FF;padding: 0 10px;font-size: 13px;
        }
        .area-right .search .point{
            border:none;
        }
    </style>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=秘钥&plugin=AMap.PlaceSearch"></script>
    <script type="text/javascript" src="js/jquery-3.1.1.min.js" ></script>
    <title>本地搜索的数据接口</title>
</head>
<body>
    <div>
        <div class="left" id="map"></div>
        <div class="left area-right">
            <div class="search">
                <div class="s-address">
                    检索地址:<input type="text" class="address" id="address" placeholder="望京SOHO"/>
                    <div onclick="doSearch();" class="btn"><img src="img/ico_btn_search.png"/></div>
                </div>
                <div class="cur_point">
                    当前坐标:<span id="s-point"></span><br/>
                    当前检索城市:<span id="s-city"></span>
                </div>
            </div>
            <div id="result">
                <!--<ul>
                    <li>
                        <div class="res-data">
                            <div class="left res-marker">
                                <span>A</span>
                            </div>
                            <div class="left res-address">
                                <div class="title">望京</div>
                                <div>地址:<span class="rout">地铁14号线; 地铁15号线</span></div>
                                <div>坐标:<span class="point">116.475304,40.004532</span></div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </li>
                </ul>-->
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
</body>
</html>
<script type="text/javascript">


    $(function(){
        
        //浏览器当前窗口文档body的高度 
        var height = $(document.body).height();
        $("#map").css("height",(height-5)+"px");
        $("#result").css("height",(height-100)+"px");
        
        $("#address").val("");
        $("#result").on("click","li",function(){
            var point = $(this).find(".point").text();
            $("#s-point").text(point);//赋值
            $("#result li").css("background-color","#fff");
            $(this).css("background-color","#f0f0f0");
        });
        
        //绑定input文本框回车事件
        $('#address').bind('keypress',function(event){
            if(event.keyCode == "13"){
               doSearch();//搜索
            }
        });
    });
    
    //高德地图API功能
    var map = new AMap.Map('map', {
        resizeEnable: true,
        zoom:11,
        center: [116.397428, 39.90923]
    });
    
    //为地图注册click事件获取鼠标点击出的经纬度坐标
    var clickEventListener = map.on('click', function(e) {
        document.getElementById("s-point").innerHTML = e.lnglat.getLng() + ',' + e.lnglat.getLat();
        marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position:[e.lnglat.getLng(),e.lnglat.getLat()]
        });
        marker.setMap(map);
    });
    
    
    var placeSearch = new AMap.PlaceSearch();  //构造地点查询类
    var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});//信息窗口
    var markers = [];//定义标注数组
    //地址查询  
    function doSearch(){
        map.remove(markers);//查询前先移除所有标注
        var address = document.getElementById("address").value;
        placeSearch.search(address, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
//               alert(JSON.stringify(result));
               var poiArr = result.poiList.pois;
               var str = "<ul>";
               for(var i=0;i<poiArr.length;i++){
                    //在地图上创建标注点
                    marker = new AMap.Marker({
                        icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png"
                    });
                    marker.setPosition(new AMap.LngLat(poiArr[i].location.lng,poiArr[i].location.lat));
                    marker.setMap(map);
                    marker.setLabel({//label默认蓝框白底左上角显示,样式className为:amap-marker-label
                        offset: new AMap.Pixel(3, 0),//修改label相对于maker的位置
                        content: String.fromCharCode(65+i)
                    });
                    marker.content = poiArr[i].name+"<br/>"+poiArr[i].address;
                    marker.on('click', markerClick);
//                    marker.emit('click', {target:marker});
                    markers.push(marker);
                       
                       str+='<li>';
                        str+='<div class="res-data">';
                            str+='<div class="left res-marker">';
                                str+='<span>'+String.fromCharCode(65+i)+'</span>';
                            str+='</div>';
                            str+='<div class="left res-address">';
                                str+='<div class="title">'+poiArr[i].name+'</div>';
                                str+='<div>地址:<span class="rout">'+poiArr[i].address+'</span></div>';
                                str+='<div>坐标:<span class="point">'+poiArr[i].location.lng+","+poiArr[i].location.lat+'</span></div>';
                            str+='</div>';
                            str+='<div class="clearfix"></div>';
                        str+='</div>';
                    str+='</li>';
               }
                str+='</ul>';
                $("#result").html(str);
                $("#s-point").text(poiArr[0].location.lng+","+poiArr[0].location.lat);
                //设置地图显示级别及中心点
                map.setZoomAndCenter(14,new AMap.LngLat(poiArr[0].location.lng,poiArr[0].location.lat));
                   //获取查询城市信息
                   map.getCity(function(res){
                       $("#s-city").text(res.province+res.city);
                   });
            
            }
        });
    }
    
    //点击标注  显示信息窗口及内容
    function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
    }
    
</script>

最后看效果,有没有发现高德和百度地图风格不太一样

高德地图实现地址检索获取结果列表和坐标第1张

免责声明:文章转载自《高德地图实现地址检索获取结果列表和坐标》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android打开相机进行人脸识别,使用虹软人脸识别引擎Spring NamedParameterJdbcTemplate 详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

百度地图api写一个有标注点有连线的地图

第一步引入资源,创建百度地图容器 <!--百度地图容器--> <div style="697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div> <script src="http://libs.baidu.com/jquery/1.9...

【python】获取高德地图省市区县列表

项目中需要用省市区来进行检索,原想高德地图肯定会有API来获得这些数据,结果没有找到,有一个接口好像可以用,但是会附带大量的边界坐标点。 所以就不如自己把高德的省市区列表扒下来,自己写接口来完成这个功能。 看到高德地图的js的demo里面有这样的展示页面:http://lbs.amap.com/api/javascript-api/example/u/20...

百度地图JavaScript API获取用户当前经纬度和详细地理位置,反之通过详细地理位置获取当前经纬度

本文并非原创,原创地址为:https://www.cnblogs.com/Can-daydayup/p/10941470.html 前言:   前段时间刚好使用了百度地图的js api定位获取用户当前经纬度并获取当前详细位置和通过当前用户详细地理位置换取用户当前经纬度坐标的功能,为了方便下次找起来方便一些自己在这里记录一下,希望也能够帮助到有需要的童鞋们!...

对互联网中常见地图的坐标系探讨

文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/。 1.背景 目前项目中使用百度地图、高德地图、谷歌中国地图、天地图的需求越来越多,这里我跟大家一起对各地图使用的坐标系做一个简单的探讨。 2.百度地图——BD-09 百度地图是在GCJ-02坐标系上,又自己对坐标加密了一次,它官方...

高德地图markers生成和点击

因为自己平时上班也是比较忙,遇到什么写什么,希望能给现在的你一些帮助,都是自己在工作中遇到的问题,给自己一个提醒,也是分享,相信很多人在做高德地图开发的时候,对于新手,官方的demo解读单个marker的生成还是很简单的,我记得官方给多了很多生成点样式的各个讲解,所以今天也是跟大家分析关系markers的生成和点击事件 首先,要知道生成marker的重要关...

Vue3.0中使用高德地图

VUE-CLI 3.0 中配置高德地图 在项目开发中,有时需要加载地图控件,这里记录下载vue-cli中引入地图控件的基本流程。 1、首先,需要注册高德开放平台的账号,并在【应用管理】页面【创建新应用】,为应用添加Key值 高德开放平台:https://lbs.amap.com/ 2、在Vue-Cli中public下的index.html加入script...