1 <html>
2 <head>
3 <title>根据地址查询经纬度</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5 <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script>
6 </head>
7 <body style="background:#CBE1FF">
8 <div style="730px;margin:auto;">
9 要查询的地址:<input id="text_" type="text" value="" style="margin-right:100px;"/>
10 查询结果(经纬度):<input id="result_" type="text" />
11 <input type="button" value="查询" onclick="searchByStationName();"/>
12 <div id="container"
13 style="position: absolute;
14 margin-top:30px;
15 730px;
16 height: 590px;
17 top: 50;
18 border: 1px solid gray;
19 overflow:hidden;">
20 </div>
21 </div>
22 </body>
23
24 <script type="text/javascript">
25 var map = new BMap.Map("container");
26 map.centerAndZoom("北京", 12);
27 map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
28 map.enableContinuousZoom(); //启用地图惯性拖拽,默认禁用
29
30 map.addControl(new BMap.NavigationControl()); //添加默认缩放平移控件
31 map.addControl(new BMap.OverviewMapControl()); //添加默认缩略地图控件
32 map.addControl(new BMap.OverviewMapControl({ isOpen: true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT })); //右下角,打开
33
34 var localSearch = new BMap.LocalSearch(map);
35 localSearch.enableAutoViewport(); //允许自动调节窗体大小
36 function searchByStationName() {
37 map.clearOverlays();//清空原来的标注
38 var keyword = document.getElementById("text_").value;
39 localSearch.setSearchCompleteCallback(function (searchResult) {
40 var poi = searchResult.getPoi(0);
41 document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat;
42 map.centerAndZoom(poi.point, 13);
43 var marker = new BMap.Marker(new BMap.Point(poi.point.lng, poi.point.lat)); // 创建标注,为要查询的地方对应的经纬度
44 map.addOverlay(marker);
45 var content = document.getElementById("text_").value + "<br/><br/>经度:" + poi.point.lng + "<br/>纬度:" + poi.point.lat;
46 var infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + content + "</p>");
47 marker.addEventListener("click", function () { this.openInfoWindow(infoWindow); });
48 // marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
49 });
50 localSearch.search(keyword);
51 }
52 </script>
53 </html>