利用百度地图定位

来源:互联网 发布:苹果一键开启蜂窝数据 编辑:程序博客网 时间:2024/05/17 02:47
百度API示例http://developer.baidu.com/map/jsdemo.htm#i1_4

1、根据搜索地址获取经纬度

<iframe style="margin-top:50px;" id="mapPage"  width="100%" height="300px" frameborder=0 src="http://apis.map.qq.com/tools/locpicker?search=1&type=1&key=GCPGBZ-DHLO3U-PQ3YVS-4BNCT-FH7KH-IIBLC&referer=myapp">
</iframe>

    window.addEventListener('message', function(event) {
        // 接收位置信息,用户选择确认位置点后选点组件会触发该事件,回传用户的位置信息
        var loc = event.data;
        if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息,需判断module是否为'locationPicker'
          txaddlocation(loc)
        }
    }, false);
    function txaddlocation(obj){
     console.log(obj);//obj对象里包含当前位置信息
    }

2、获取当前的经纬度
2.1、引入<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=WCKHtQ7E3nZBTBuV2c3PQQosSDD1Tbf4TuI"></script>
2.2、页面加入<div id="allmap"></div>标签

2.3、js定位代码

<script>

var map = new BMap.Map("allmap");

var point = new BMap.Point('','');

map.centerAndZoom(point,12);

 

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);

alert('您的位置:'+r.point.lng+','+r.point.lat);

}else {

alert('暂时无法获取当前位置!');

}

},{enableHighAccuracy: true})

</script>

注:获取当前位置一定要在手机上测试(精确定位一般都是在移动端),在pc误差很大。

3、php 处理逻辑

3.1、获取两个经纬度之间的距离
function distanceByLnglat($lng1,$lat1,$lng2,$lat2)
{
     $radlat1 = Rad($lat1);
     $radlat2 = Rad($lat2);
     $a = $radlat1 - $radlat2;
     $b = Rad($lng1) - Rad($lng2);
     $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radlat1) * cos($radlat2) * pow(sin($b / 2), 2)));
     $s = $s * 6378137.0;// 取WGS84标准参考椭球中的地球长半径(单位:m)
     $s = round($s * 10000) / 10000;
    return $s;
}
function Rad($d)
{
    return $d * M_PI / 180.0;
}

3.2、计算经纬度范围
$lon当前位置经度
$lat当前位置纬度
$raidus半径
function getAround($lon, $lat, $raidus)
    {
    $PI = 3.14159265;
    $EARTH_RADIUS = 6378137;
    $RAD = $PI / 180.0;
    $latitude = $lat;
    $longitude = $lon;
    $degree = (24901 * 1609) / 360.0;
    $raidusMile = $raidus;
    $dpmLat = 1 / $degree;
    $data = array();
    $radiusLat = $dpmLat * $raidusMile;
    $minLat = $latitude - $radiusLat;
    $maxLat = $latitude + $radiusLat;
    $data["maxLat"] = $maxLat;
    $data["minLat"] = $minLat;
    $mpdLng = $degree * cos($latitude * ($PI / 180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng * $raidusMile;
    $minLng = $longitude - $radiusLng;
    $maxLng = $longitude + $radiusLng;
    $data["maxLng"] = $maxLng;
    $data["minLng"] = $minLng;
    return $data;
}

3.3、根据地址获取经纬度:
方法一、
$return=file_get_contents('http://api.map.baidu.com/geocoder/v2/output=json&ak=jCIHLQfopBqCvHlQDI451CQrGrxs7ELGGVX&address='.$address);

方法二、
public function getAreaLngLatOp(){
$address = $_GET['address'];
$url = "http://api.map.baidu.com/geocoder/v2/";
$data = array(
'output'=> 'json',
'ak'=>'WCKHtQ7E3nZBTBuV2c3PQQosSDD1Tbf4TuI',
'callback'=>'showLocation',
'address'=>str_replace(' ', '',$address),
'city'=>'',
);
$result = $this->getLocation($url,$data);
$location = $result['result']['location'];
output_data($location);
}
public function getLocation($url,$data){
$ch = curl_init ();
// print_r($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec ( $ch );
//var_dump(curl_error($ch));
curl_close ( $ch );
$data = json_decode($return,true);
return $data;
}

原创粉丝点击