$val["id"], "parent_id" => $parentid); $array[] = $indata; if (isset($val["children"])) { $children = setlist($val["children"], $val["id"]); if ($children) { $array = array_merge($array, $children); } } } return $array; } //初始化树形结构 function infiniteTree($data, $pid = 0) { if (!is_array($data) || empty($data)) return false; $tree = array(); foreach ($data as $k => $v) { // 找出所有的儿子 if ($v['parent_id'] == $pid) { // 将儿子数据赋值给sub键,递归看看儿子还有没有孙子 $v['children'] = infiniteTree($data, $v['id']); $tree[] = $v; // 删除遍历过的数组数据 unset($data[$k]); } } return $tree; } //对象转数组 function object_array($array) { if (is_object($array)) { $array = (array)$array; } if (is_array($array)) { foreach ($array as $key => $value) { $array[$key] = object_array($value); } } return $array; } //求地图中心点 function GetCenterFromDegrees($data) { if (!is_array($data)) return FALSE; $num_coords = count($data); $X = 0.0; $Y = 0.0; $Z = 0.0; foreach ($data as $coord) { $lat = deg2rad((float)$coord['lat']); $lon = deg2rad((float)$coord['lng']); $a = cos($lat) * cos($lon); $b = cos($lat) * sin($lon); $c = sin($lat); $X += $a; $Y += $b; $Z += $c; } $X /= $num_coords; $Y /= $num_coords; $Z /= $num_coords; $lon = atan2($Y, $X); $hyp = sqrt($X * $X + $Y * $Y); $lat = atan2($Z, $hyp); return array('lat' => rad2deg($lat), 'lng' => rad2deg($lon)); } //计算两点之间坐标距离 单位 米 function GetDistance($lat1, $lng1, $lat2, $lng2) { $radLat1 = $lat1 * (PI / 180); $radLat2 = $lat2 * (PI / 180); $a = $radLat1 - $radLat2; $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180)); $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); $s = $s * EARTH_RADIUS; //$s = round($s * 10000) / 10000; return $s * 1000; } //获取一个多边形中心点 到此多边形个点的最大距离 单位 米 function maxDistance(array $centre, array $fence) { $distanceArr = array(); foreach ($fence as $v) { $distanceArr[] = GetDistance($centre['lat'], $centre['lng'], $v['lat'], $v['lng']); } return max($distanceArr); } // lat转latitude lng转longitude function latToLatitude($str) { if (is_array($str)) { $str = json_encode($str); } $pattern_lat = '/lat/'; $replace_lat = 'latitude'; $str_lat = preg_replace($pattern_lat, $replace_lat, $str); $pattern_lng = '/lng/'; $replace_lng = 'longitude'; $str = preg_replace($pattern_lng, $replace_lng, $str_lat); return $str; } // latitude转lat longitude转lng function latitudeTolat($str) { if (is_array($str)) { $str = json_encode($str); } $pattern_lat = '/latitude/'; $replace_lat = 'lat'; $str_lat = preg_replace($pattern_lat, $replace_lat, $str); $pattern_lng = '/longitude/'; $replace_lng = 'lng'; $str = preg_replace($pattern_lng, $replace_lng, $str_lat); return $str; } //高德地图绘制取精确数据 (前端已处理) function getAccurate(array $arr) { $data = []; foreach ($arr as $v) { $bata = []; $bata['lat'] = $v['P']; $bata['lng'] = $v['O']; $data[] = $bata; } return $data; } // 渲染高德地图, 电子围栏数据转换函数 function toGaodeFenceData($str) { $arr = json_decode($str); $res = []; foreach ($arr as $v) { $arr2 = [$v->longitude, $v->latitude]; $res[] = $arr2; } $res = json_encode($res); return $res; } // 渲染高德, 中心点数据转换函数 function toGaodeCentreData($str) { $arr = json_decode($str); $res = [$arr->longitude, $arr->latitude]; $res = json_encode($res); return $res; } /** * 获取图片的Base64编码(不支持url) * @date 2017-02-20 19:41:22 * * @param $img_file 传入本地图片地址 * * @return string */ function imgToBase64($img_file) { $img_base64 = ''; if (file_exists($img_file)) { $app_img_file = $img_file; // 图片路径 $img_info = getimagesize($app_img_file); // 取得图片的大小,类型等 //echo '
' . print_r($img_info, true) . '