首页 > 代码库 > 百度地图Api定位当前的位置

百度地图Api定位当前的位置

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>百度地图</title>
  <script type="text/javascript" src="http://api.map.baidu.com/api?ak=PlhFWpA02aoURjAOpnWcRGqw7AI8EEyO&v=2.0&services=false"></script> 
 </head>
 <body>
    <style>
      body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
      #allmap{height:500px;width:100%;}
    </style>

    <div id="allmap"></div>

    <script type="text/javascript">
        $(function(){
            var map = new BMap.Map("allmap");
            var point = new BMap.Point(116.501573, 39.900877);
            map.centerAndZoom(point, 16)
            // 定位对象
            var geoc = new BMap.Geocoder();
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    var mk = new BMap.Marker(r.point);
                    map.addOverlay(mk);
                    map.panTo(r.point);
                    //$("#start_point").val(r.point.lng+‘,‘+r.point.lat);
                    alert("当前位置经度为:"+r.point.lng+"纬度为:"+r.point.lat);
                    setLocation(r.point);
                }else {
                    $("#start").attr("placeholder","请输入您的当前位置")
                    alert(无法定位到您的当前位置,导航失败,请手动输入您的当前位置!+this.getStatus());
                }
            },{enableHighAccuracy: true});

            //获取地理位置的函数
            function setLocation(point){
                geoc.getLocation(point, function(rs){
                    var addComp = rs.addressComponents;
                    var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                    //$("#start").val(result);
                    //$("#start_location").val(result);
                    alert("当前的位置为:"+result);
                });
            }
        });
    </script>
 </body>
</html>

 

百度地图Api定位当前的位置