您现在的位置:首页 >> 前端 >> 内容

百度接口示例:根据地址获取该地址的经纬度

时间:2017/3/16 9:24:00 点击:

  核心提示:百度接口示例:根据地址获取该地址的经纬度。html xmlns=https://www.w3.org/1999/xhtmlheadtitle根据地址查询经纬度/titlemeta http-equiv...

百度接口示例:根据地址获取该地址的经纬度。
 

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <title>根据地址查询经纬度</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak="></script>
</head>
<body style="background:#CBE1FF">
    <p style="width:730px;margin:auto;">   
        要查询的地址:<input id="text_" type="text" value="天安门广场" style="margin-right:100px;"/>
        查询结果(经纬度):<input id="result_" type="text" />
        <input type="button" value="查询" onclick="searchByStationName();"/>
        <p id="container"
            style="position: absolute;
                margin-top:30px; 
                width: 730px; 
                height: 590px; 
                top: 50; 
                border: 1px solid gray;
                overflow:hidden;">
        </p>
    </p>
</body>
</html>
    <script type="text/javascript">
        var map = new BMap.Map("container");
        map.centerAndZoom("北京", 12);
        map.enableScrollWheelZoom();    //启用滚轮放大缩小,默认禁用
        map.enableContinuousZoom();    //启用地图惯性拖拽,默认禁用  
        map.addControl(new BMap.NavigationControl());  //添加默认缩放平移控件
        map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
        //map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT }));   //右下角,打开
        var distance = map.getDistance(new BMap.Point(116.398045,39.913845),new BMap.Point(116.407855,39.914191));//取两个经纬度间的距离
        alert(distance);
        var localSearch = new BMap.LocalSearch(map);
        localSearch.enableAutoViewport(); //允许自动调节窗体大小
        function searchByStationName() {
            map.clearOverlays();//清空原来的标注
            var keyword = document.getElementById("text_").value;
            localSearch.setSearchCompleteCallback(function (searchResult) {
                var poi = searchResult.getPoi(0);
                document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
                map.centerAndZoom(poi.point, 13);
                var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat));  // 创建标注,为要查询的地址对应的经纬度
                map.addOverlay(marker);
            });
            localSearch.search(keyword);
        } 
    </script>

 

作者:网络 来源:dreamboycx