1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 获取距离某个坐标点最近的几个坐标(计算距离)

获取距离某个坐标点最近的几个坐标(计算距离)

时间:2020-05-12 19:48:36

相关推荐

获取距离某个坐标点最近的几个坐标(计算距离)

function getNearlySite(){// $geohash = new \Geohash();//中心点位$id = params('id');$arr = Db::table('enterprises')->field('id,lat84,lon84')->where('id','=',$id)->select();//坐标$data = Db::table('sites')->field('id,lat,lon,site_name')->select();//算出实际距离foreach($data as $key=>$val){$distance = self::getDistance($arr[0]['lat84'],$arr[0]['lon84'],$val['lat'],$val['lon']);$data[$key]['distance'] = $distance;//排序列$sortdistance[$key] = $distance;}//距离排序array_multisort($sortdistance,SORT_ASC,$data);//最近3个点$lists = array();$weeks = getWeek();foreach($data as $ke=>$va){if($ke<3){foreach ($weeks as $k=>$v){$aqi = Db::table('air_quality')->field('avg(PM10) as AQI,SUBSTRING(add_time,1,10) as add_time,site_code')->where('site_code','=',$va['id'])->where('add_time','like',$v.'%')->group('SUBSTRING(add_time,1,10)')->order('add_time','desc')->limit(7)->select();if($aqi){$lists['name'.$ke]['AQI'][] = $aqi[0]['AQI'];$lists['name'.$ke]['time'][] = $v;}else{$lists['name'.$ke]['AQI'][] = 0;$lists['name'.$ke]['time'][] = $v;}}$lists['name'.$ke]['name'][] = $va['site_name'];}}return getDates($lists);}//计算距离function getDistance($latitude1, $longitude1, $latitude2, $longitude2){$earth_radius = 6371000; //approximate radius of earth in meters$dLat = deg2rad($latitude2 - $latitude1);$dLon = deg2rad($longitude2 - $longitude1);/*Using theHaversine formula/wiki/Haversine_formula/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe验证:百度地图 /map/jsdemo.htm#a6_1calculate the distance*/$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);$c = 2 * asin(sqrt($a));$d = $earth_radius * $c;return round($d); //四舍五入}

这个就是直接能用的,本来找的是一个geohash算法的,但是貌似这里没用到 !就自己修修改改发上来了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。