php地图经纬度相关算法

1.高德地图点聚合算法(网格法)

根据地图上的范围(取左上角与右下角的经纬度 )
getmain 根据范围和数量平均取值生成网格
getzxd 取出网格点的经纬度的平均值
getzxdnum 获取范围内数据的点

/*
 * 返回所有的站点信息
 */

public function getmain() {
    $data = I('get.');
    if ($data['lat_r'] && $data['lat_l']) {

        $lat1 = $data['lat_r'];
        $lat2 = $data['lat_l'];
        $lng1 = $data['lng_l'];
        $lng2 = $data['lng_r'];
        $num = 200;
        $lat_dr = ($lat2 - $lat1) / $num;
        $lng_dr = ($lng2 - $lng1) / $num;
        for ($i = $lat1; $i < $lat2; $i+=$lat_dr) {
            $aa_yd = $i + $lat_dr;
            for ($n = $lng1; $n < $lng2; $n+=$lng_dr) {
                $bb_yd = $n + $lng_dr;
                //$re = $this->getzxd($i, $aa_yd, $n, $bb_yd);
                $kkk = $this->getzxdnum($i, $aa_yd, $n, $bb_yd);
                $re = $this->getzxd($kkk);
                $re['num'] = count($kkk);
                if ($re['num'] > 0) {
                    $conect[] = $re;
                }
            }
        };
        unset($i);
        unset($n);
        unset($bb_yd);
        unset($aa_yd);
    }
    $cc = $this->savejh($conect);
    //$conect = array_values($conect);
    echo json_encode($cc);
}

//获取中心点
private function getzxd($date) {
    $lat = '';
    $lng = '';
    $num = count($date);
    foreach ($date as $key => $value) {
        $lng += $value['longitude'];
        $lat += $value['latitude'];
    }
    $kk['lat_cc'] = $lat / $num;
    $kk['lng_cc'] = $lng / $num;
    return $kk;
}
//获取数量
private function getzxdnum($lat1, $lat2, $lng1, $lng2) {
    $list = M('Charging')->field('id,latitude,longitude')->where("latitude>{$lat1} and latitude<{$lat2} and longitude>{$lng1} and longitude<{$lng2}")->select();
    return $list;
}

2.根据地图上两点的经纬度算出距离

/**
 * @desc 根据两点间的经纬度计算距离
 * @param float $lat 纬度值
 * @param float $lng 经度值
 */
public function getDistance($lat1, $lng1, $lat2, $lng2) {
    $earthRadius = 6378.137; //approximate radius of earth in meters

    $lat1 = ($lat1 * pi() ) / 180;
    $lng1 = ($lng1 * pi() ) / 180;

    $lat2 = ($lat2 * pi() ) / 180;
    $lng2 = ($lng2 * pi() ) / 180;

    $calcLongitude = $lng2 - $lng1;
    $calcLatitude = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;

    return round($calculatedDistance, 2);
}

3.本地数据聚合算法

查询范围内聚合数据的平均值

SELECT `id`,count(*) as cnt, substr( longitude,0,4) as pjing, substr(latitude,0,3) as pwei, avg(longitude) as pjing2, avg(latitude) as pwei2 FROM pile  GROUP BY pjing2,pwei2

你可能感兴趣的:(php地图经纬度相关算法)