php騰訊地圖 計算距離

一:需要接口

 /*
     * 騰訊地圖計算距離
     *  $from:起點
     *  $to : 終點
     *  $key:key
     * */
    function TX_Map_Api_distance($form,$to){

        $url = "http://apis.map.qq.com/ws/distance/v1/?mode=driving&from=".$form."&to=".$to."&key=".$key;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch);
        curl_close($ch);
        $output =  json_decode($output,true);
        if($output['status']==0){
            return $output['result']['elements'][0]['distance'];

        }else{
            return '位置出錯';
        }

    }

二,直接計算

/*
	 * 地圖計算距離
	 *  $lat1:起點緯度
	 *  $lng1 : 起點經度
	 *  $lat2:終點緯度
	 *  $lng2 : 終點經度
	 * */
	function TX_Map_Api_distance($lat1,$lng1,$lat2,$lng2){

		// 將角度轉爲狐度
		$radLat1 = deg2rad($lat1);// deg2rad()函數將角度轉換爲弧度
		$radLat2 = deg2rad($lat2);
		$radLng1 = deg2rad($lng1);
		$radLng2 = deg2rad($lng2);

		$a = $radLat1 - $radLat2;
		$b = $radLng1 - $radLng2;

		$s = 2 * asin(sqrt(pow(sin($a / 2), 2)+cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;

		return $s;


	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章