百度地图JavaScript API经纬度查询-MAP

时间:2021-06-17 09:39:56

百度地图JavaScript API经纬度查询-MAP-ABCDEFGHIJKMHNOPQRSTUVWXYZ:

搜索:<input type="text" size="20" name="keyword" id="keyword" /> <input type="submit" value="提交" onclick="MblogDotccMap(document.getElementById('keyword').value)" />

<!--
更多技术分享请看博客:http://blog.csdn.net/fuyifang
-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>百度地图Demo-更多技术分享请看博客:http://blog.csdn.net/fuyifang</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
</head>
<body>
    <div style="width: 520px; height: 340px; border: 1px solid gray" id="container">
    </div>
</body>
</html>
<script type="text/javascript">

    var map = new BMap.Map("container"); //初始化地图

    var opts = { type: BMAP_NAVIGATION_CONTROL_LARGE }; //初始化地图控件
    map.addControl(new BMap.NavigationControl(opts));

    var point = new BMap.Point(121.48, 31.22); //初始化地图中心点
    var marker = new BMap.Marker(point); //初始化地图标记
    marker.enableDragging(); //标记开启拖拽

    var gc = new BMap.Geocoder();
    //添加标记拖拽监听
    marker.addEventListener("dragend", function (e) {
        //获取地址信息
        gc.getLocation(e.point, function (rs) {
            showLocationInfo(e.point, rs);
        });
    });

    //添加标记点击监听
    marker.addEventListener("click", function (e) {
        gc.getLocation(e.point, function (rs) {
            showLocationInfo(e.point, rs);
        });
    });

    map.centerAndZoom(point, 15); //设置中心点坐标和地图级别
    map.addOverlay(marker); //将标记添加到地图中

    //显示地址信息窗口
    function showLocationInfo(pt, rs) {
        var opts = {
            width: 250, //信息窗口宽度
            height: 100, //信息窗口高度
            title: "" //信息窗口标题
        }

        var addComp = rs.addressComponents;
        var addr = "当前位置:" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + "<br />";
        addr += "纬度: " + pt.lat + ", " + "经度:" + pt.lng;
        //alert(addr);

        var infoWindow = new BMap.InfoWindow(addr, opts); //创建信息窗口对象
        marker.openInfoWindow(infoWindow);
    }
    function MblogDotccMap(keyword) {
        //搜索
        var local = new BMap.LocalSearch(map,
         { renderOptions: { map: map, panel: "results_info" }
          }
         );

        map.panBy(point);

        local.search(keyword);

        local.getResults()

        local.setSearchCompleteCallback(function (searchResult) {
            var poi = searchResult.getPoi(0);
            //alert(poi.point.lng+"   "+poi.point.lat);
            //document.getElementById("info").innerHTML = "<strong>" + keyword + "</strong>" + "坐标:" + poi.point.lng + "," + poi.point.lat;
        });

    }

</script>
<div id="results_info"></div>
搜索:<input type="text" size="20" name="keyword" id="keyword" /> <input type="submit" value="提交" onclick="MblogDotccMap(document.getElementById('keyword').value)" />
<!--
更多技术分享请看博客:http://blog.csdn.net/fuyifang
-->