百度地图---鼠标点击地图获取位置省市县

jsp:

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=百度密钥"></script>

<body>

<div class="area" id="allmap"></div>
              <div class="layui-inline ">
<label class="layui-form-label"><span style="color: red">*</span>地址</label>
<div class="layui-input-inline">
<input type="text" id="pointaddress" name="pointaddress" class="layui-input userName"
value="" lay-verify="required" placeholder="请点击上方地图选择您的设备位置,地图可缩放">
   </div>
</div>
<div class="layui-inline lie">
<label class="layui-form-label">所属省份</label>
<div class="layui-input-inline">
<input type="text" id="provinces" name="provinces" class="layui-input userName"
value="">
   </div>
</div>
<div class="layui-inline lie">
<label class="layui-form-label">所属市区</label>
<div class="layui-input-inline">
<input type="text" id="city" name="city" class="layui-input userName"
value="">
   </div>
</div>
<div class="layui-inline lie">
<label class="layui-form-label">所属县区</label>
<div class="layui-input-inline">
<input type="text" id="town" name="town" class="layui-input userName"
value="">
   </div>

</div>

</body>

<script type="text/javascript">
var map = new BMap.Map('allmap');
var point=new BMap.Point(109.404269,30.916042);//中心点
map.centerAndZoom(point, 6);//中心点经纬度,缩放级别4
var geoc = new BMap.Geocoder();   
map.enableDragging();   //启用拖拽
map.enableScrollWheelZoom(true);//启用滚轮放大缩小
var  mapStyle ={ 
       features: ["road", "building","water","land"],//隐藏地图上的poi
       style : "green"  //设置地图风格为高端黑dark 
   }
map.setMapStyle(mapStyle);
function checkhHtml5()   
       {   
           if (typeof(Worker) === "undefined")     
           {   
               if(navigator.userAgent.indexOf("MSIE 9.0")<=0)  
  {  
alert("定制个性地图示例:IE9以下不兼容,推荐使用百度浏览器、chrome、firefox、safari、IE10");   
  }  
           }  
       }
checkhHtml5();          
//单击获取点击的经纬度
map.addEventListener("click",function(e){
var address=e.point.lng + "," + e.point.lat;
$("#pointaddress").attr("value",address);
var pt = e.point;
geoc.getLocation(pt, function(rs){
var addComp = rs.addressComponents;
$("#provinces").attr("value",addComp.province);
$("#city").attr("value",addComp.city);
$("#town").attr("value",addComp.district);
//省、市、区、街道、街道号
//alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});        
});
</script> 

猜你喜欢

转载自blog.csdn.net/qq_36357608/article/details/80962319
今日推荐