PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通PHP建站技术分享-从入门到精通

QQ:420220301 微信/手机:150-3210-7690
当前位置:首页 > CMS教程 > Fastadmin

校验经纬度是否在经纬度数组范围内,以及获取经纬度中心点

管理员 2024-12-14
Fastadmin
10

刚好最近做过这种业务需求,就分享给大家,希望能帮助到你

代码:

// 校验坐标$position = ['lat' => 31.56868661659311, 'lng' => 104.85818892344969];$position = ['lat' => 31.613626970323, 'lng' => 104.650955200195];// 多边形坐标$polygon = [    ['lat' => 31.613626970323, 'lng' => 104.650955200195],    ['lat' => 31.431592610480, 'lng' => 104.556198120117],    ['lat' => 31.351877796703, 'lng' => 104.781417846680],    ['lat' => 31.532896662757, 'lng' => 104.984664916992],    ['lat' => 31.673252240968, 'lng' => 104.860382080078],];/** * 校验经纬度是否在多边形内 */function vaildatePoint($position = [], $polygon = []){    $maxY = $maxX = 0;    $minY = $minX = 9999;    foreach ($polygon as $item) {        if ($item['lat'] > $maxX) $maxX = $item['lat'];        if ($item['lat'] < $minX) $minX = $item['lat'];        if ($item['lng'] > $maxY) $maxY = $item['lng'];        if ($item['lng'] < $minY) $minY = $item['lng'];    }    if ($position['lat'] < $minX || $position['lat'] > $maxX || $position['lng'] < $minY || $position['lng'] > $maxY) {        return false;    }    return true;}var_dump(vaildatePoint($position, $polygon));/** * 经纬度数组中心点获取 */function get_center_from_degrees($data = []) {    if (empty($data) || !is_array($data)) return [30.456398, 106.633369];    $num_coords = count($data);    $X          = 0.0;    $Y          = 0.0;    $Z          = 0.0;    foreach ($data as $coord){        $lat = deg2rad((float)$coord['lat']);        $lng = deg2rad((float)$coord['lng']);        $a   = cos($lat) * cos($lng);        $b   = cos($lat) * sin($lng);        $c   = sin($lat);        $X   += $a;        $Y   += $b;        $Z   += $c;    }    $X /= $num_coords;    $Y /= $num_coords;    $Z /= $num_coords;    $lng = atan2($Y, $X);    $hyp = sqrt($X * $X + $Y * $Y);    $lat = atan2($Z, $hyp);    return [rad2deg($lat), rad2deg($lng)];}var_dump(get_center_from_degrees($polygon));

希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。

相关推荐

扫码关注

qrcode

QQ交谈

回顶部