<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>根据地址查询经纬度</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=申请的ak值"></script>
<script type="text/javascript" src="http://wap.bingjun.cn/js/jquery-1.9.2.min.js"></script>
<script type="text/javascript" src="http://wap.bingjun.cn/js/layer/layer.js"></script>
<script type="text/javascript">
$(function(){
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 localSearch = new BMap.LocalSearch(map);
localSearch.enableAutoViewport(); //允许自动调节窗体大小
//根据地址查询移除事件
$("#text_").blur(function(){
searchByStationName();
});
});
</script>
<script type="text/javascript">
var text;
var geolocation = new BMap.Geolocation();
var myGeo = new BMap.Geocoder();
geolocation.getCurrentPosition(function(result){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var pt=result.point;
myGeo.getLocation(pt, function(result){
if (result){
var addComp = result.addressComponents;
//获取地址详情
var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
text=address;
$("#text_").val(text);
searchByStationName();
}
});
}
});
//获取经纬度显示到文本(可以自由发挥)
function searchByStationName() {
var map = new BMap.Map("container");
var local = new BMap.LocalSearch(map, {
renderOptions: { map: map }
});
var keyword = document.getElementById("text_").value;
local.setSearchCompleteCallback(function (searchResult) {
var poi = searchResult.getPoi(0);
document.getElementById("result_").value = poi.point.lng + "," + poi.point.lat; //获取经度和纬度,将结果显示在文本框中
map.centerAndZoom(poi.point, 13);
});
local.search(keyword);
}
</script>
</head>
<body style="background:#CBE1FF">
<div style="width:1200px;margin:auto;">
要查询的地址:<input id="text_" type="text" value="" style="margin-right:100px;"/>
查询结果(经纬度):<input id="result_" type="text" />
<div id="container" style="width: 1200px; height: 900px;border: 1px solid gray;">
</div>
</div>
</body>
</html>