关于地址转坐标经纬度,之间的距离的总结

    /**
	 * 根据地址 获取经纬度
	 * @param $address
	 * @return mixed
	 */
	function addresstolatlag($address){
	    $url='http://restapi.amap.com/v3/geocode/geo?address='.$address.'&key=d5d5c6a7fcc168491804990c7f55ef08';
	    if($result=file_get_contents($url))
	    {
	        $result = json_decode($result,true);
	        //判断是否成功
	        if(!empty($result['count'])){
	            return  explode(',',$result['geocodes']['0']['location']);
	        }else{
	            return false;
	        }
	    }
	}

	/**
	 * 根据经纬度 获取地址
	 * @param $address23.2322,12.15544
	 * @return mixed
	 */
	function getaddress($address){
	    $url="http://restapi.amap.com/v3/geocode/regeo?output=json&location=".$address."&key=d5d5c6a7fcc168491804990c7f55ef08";
	    if($result=file_get_contents($url))
	    {
	        $result = json_decode($result,true);
	        if(!empty($result['status'])&&$result['status']==1){
	            return $result['regeocode']['formatted_address'];
	        }else{
	            return false;
	        }
	    }
	}
    /**
     * 计算某个经纬度的周围某段距离的正方形的四个点
     *
     * @param
     *            radius 地球半径 平均6371km
     * @param
     *            lng float 经度
     * @param
     *            lat float 纬度
     * @param
     *            distance float 该点所在圆的半径,该圆与此正方形内切,默认值为1千米
     * @return array 正方形的四个点的经纬度坐标
     */
    public function returnSquarePoint($lng, $lat, $distance = 1, $radius = 6371)
    {
        $dlng = 2 * asin(sin($distance / (2 * $radius)) / cos(deg2rad($lat)));
        $dlng = rad2deg($dlng);
        
        $dlat = $distance / $radius;
        $dlat = rad2deg($dlat);
        
        return array(
            'left-top' => array(
                'lat' => $lat + $dlat,
                'lng' => $lng - $dlng
            ),
            'right-top' => array(
                'lat' => $lat + $dlat,
                'lng' => $lng + $dlng
            ),
            'left-bottom' => array(
                'lat' => $lat - $dlat,
                'lng' => $lng - $dlng
            ),
            'right-bottom' => array(
                'lat' => $lat - $dlat,
                'lng' => $lng + $dlng
            )
        );
    }

 /**
     *  计算两组经纬度坐标 之间的距离
     *   params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
     *   return m or km
     */
	 
     function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
       $radLat1 = $lat1 * PI ()/ 180.0;   //PI()圆周率
       $radLat2 = $lat2 * PI() / 180.0;
       $a = $radLat1 - $radLat2;
       $b = ($lng1 * PI() / 180.0) - ($lng2 * PI() / 180.0);
       $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) *                     
       pow(sin($b/2),2)));
       $s = $s * 6378.137;
       $s = round($s * 1000);
       if ($len_type --> 1)
       {
           $s /= 1000;
       }
       return round($s, $decimal);
	}


/**
	 * 获取维修网点列表
	 */
	public function get_assemble_listfj($start){

		$where = ' ';

			$where .=' and lat > '.$start['right-bottom']['lat'];


			$where .=' and lat < '.$start['left-top']['lat'];


			$where .=' and lng > '.$start['left-top']['lng'];


			$where .=' and lng < '.$start['right-bottom']['lng'];

		$sql = ' SELECT * FROM ' . $GLOBALS['ecs']->table('assemble') . (' WHERE status = 1  '.$where);
		return $GLOBALS['db']->getAll($sql);
	}

使用实例:


        
        //将地址转为坐标数组
		$array = $this->addresstolatlag($consignee['region'].$consignee['address']);
		//取方圆10KM坐标
		$start = $this->returnSquarePoint($array[0], $array[1]);
		//取方圆10KM的维修网点列表
		$assemble_list = $this->get_assemble_listfj($start);
		if(empty($assemble_list)){
			$assemble_list = $this->get_assemble_list($consignee['province'],$consignee['city'],$consignee['district']);
		}

		foreach ($assemble_list as $k => $v) {
			//计算距离	GetDistance($lat1, $lng1, $lat2, $lng2);
			$distance = $this->GetDistance($array[1], $array[0], $v['lat'], $v['lng']);
			$assemble_list[$k]['distance'] = $distance;
		}

 

你可能感兴趣的:(php)