百度地图通过地址解析经纬度、在通过经纬度解析地址。效果图如下:
代码示例:
var map = new BMap.Map("map"); map.centerAndZoom(new BMap.Point(116.404, 39.915), 12); //定位到当前城市 function myFun(result){ var cityName = result.name; map.setCenter(cityName); } var myCity = new BMap.LocalCity(); myCity.get(myFun); //默认有个点 var market; var myGeo = new BMap.Geocoder(); var dbPoint = new BMap.Point(lng, lat); map.centerAndZoom(dbPoint, 16); market = new BMap.Marker(dbPoint); map.addOverlay(market); market.enableDragging(true); //移动标注点 market.addEventListener("dragend",function(){ var newPoint = market.getPosition(); $("#lat").val(newPoint.lat); $("#lng").val(newPoint.lng); myGeo.getLocation(newPoint, function(rs){ var addComp = rs.addressComponents; $("#address").val(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber); $("#province").val(addComp.province.replace(/省/, '')); $("#city").val(addComp.city.replace(/市/, '')); $("#district").val(addComp.district); }); }); //通过点击获取坐标进行操作 $(".getPos").click(function(){ var address = $("#address").val(); if($.isEmpty(address)) { easyDialog.open({ container : { content : '地址栏不能为空' }, autoClose : 2000 }); } else { map.removeOverlay(market); myGeo.getPoint(address, function(point) { if (point) { $("#lat").val(point.lat); $("#lng").val(point.lng); map.centerAndZoom(point, 16); market = new BMap.Marker(point); map.addOverlay(market); myGeo.getLocation(point, function(rs){ var addComp = rs.addressComponents; $("#address").val(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber); $("#province").val(addComp.province.replace(/省/, '')); $("#city").val(addComp.city.replace(/市/, '')); $("#district").val(addComp.district); }); market.enableDragging(true); market.addEventListener("dragend",function(){ var newPoint = market.getPosition(); $("#lat").val(newPoint.lat); $("#lng").val(newPoint.lng); myGeo.getLocation(newPoint, function(rs){ var addComp = rs.addressComponents; $("#address").val(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber); $("#province").val(addComp.province.replace(/省/, '')); $("#city").val(addComp.city.replace(/市/, '')); $("#district").val(addComp.district); }); }); } else { easyDialog.open({ container : { content : '无法获取位置' }, autoClose : 2000 }); } }); } }); map.addControl(new BMap.NavigationControl());