百度地图根据城市获取经纬度

<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.5/layui.js"></script>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=AnMOlYHxzVMkWfdjS6bFuxDk"></script>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point({$data['lng']}, {$data['lat']});
map.centerAndZoom('成都', 11);
map.enableScrollWheelZoom(true);
//点击返回
var geoc = new BMap.Geocoder();
map.addEventListener("click", function (e) {
    var pt = e.point;
    var input = document.getElementById('wd');
    var input2 = document.getElementById('jd');
    var address = document.getElementById("txtaddress");
    //返回坐标
    input.value  = pt.lng;
    input2.value = pt.lat;
    //点击坐标返回地址
    geoc.getLocation(pt, function (rs) {
        var addComp = rs.addressComponents;
        var addresstext = "";
        if (addComp.province == addComp.city) {
           addresstext = (addComp.province + addComp.district + addComp.street + addComp.streetNumber);
        }else {
           addresstext = (addComp.province + addComp.city + addComp.district +addComp.street + addComp.streetNumber);
        }
       address.value = addresstext;
    });
});

map.addControl(new BMap.NavigationControl());
var local = new BMap.LocalSearch(map, {
   renderOptions: { map: map }
});
theLocation();
function theLocation() {
    var address = document.getElementById("txtaddress").value;
    if (address != "") {
      local.search(address);
    }
}
//确定选择
function Determine() {
    var wd = document.getElementById('wd');
    var jd = document.getElementById('jd');
    var address = document.getElementById("txtaddress");
    if (!wd.value || !jd.value || !address.value) {
      alert('请确定地址信息'); return false;
    }
    alert('ok');
}

 

posted @ 2021-08-03 16:58  叫我星宇  阅读(474)  评论(0编辑  收藏  举报