当前位置: 首页 > news >正文

PHP 经纬度相关计算 坐标点之间的距离

根据两个坐标点,计算两点之间的距离

/***  @desc 根据两点间的经纬度计算距离*  @param float $lat 纬度值*  @param float $lng 经度值*/public static function getDistance($lat1, $lng1, $lat2, $lng2){$earthRadius = 6371000; //approximate radius of earth in meters/*Convert these degrees to radiansto work with the formula*/$lat1 = ($lat1 * pi() ) / 180;$lng1 = ($lng1 * pi() ) / 180;$lat2 = ($lat2 * pi() ) / 180;$lng2 = ($lng2 * pi() ) / 180;/*Using the Haversine formulahttp://en.wikipedia.org/wiki/Haversine_formula calculate the distance*/$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);}

计算某个经纬度的周围某段距离的正方形的四个点

/***计算某个经纬度的周围某段距离的正方形的四个点**@param lng float 经度*@param lat float 纬度*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米*@return array 正方形的四个点的经纬度坐标*/public static function returnSquarePoint($lat, $lng, $distance = 1500){$dlng =  2 * asin(sin($distance / (2 * self::EARTH_RADIUS)) / cos(deg2rad($lat)));$dlng = rad2deg($dlng);$dlat = $distance/self::earthRadius;$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));}

判断经纬度是否在points组成的多边形内,凹凸多边形都适合

public static function isInPoly($lat, $lng, $points) {if (count($points) < 3) {return false;}$sum = 0;$count = count($points);for ($i = 0; $i < $count; $i++) {if ($i == $count - 1) {$lng1 = $points[$i]['lng'];$lat1 = $points[$i]['lat'];$lng2 = $points[0]['lng'];$lat2 = $points[0]['lat'];} else {$lng1 = $points[$i]['lng'];$lat1 = $points[$i]['lat'];$lng2 = $points[$i+1]['lng'];$lat2 = $points[$i+1]['lat'];}if (($lat >= $lat1 && $lat < $lat2) || ($lat >= $lat2 && $lat < $lat1)) {if (abs($lat1 - $lat2) > 0) {$dlng = $lng1 - (($lng1 - $lng2) * ($lat1 - $lat)) / ($lat1 - $lat2);if ($dlng < $lng) {$sum++;}}}}if ($sum % 2 != 0)return true;return false;}

根据地址查到经纬度

public function getLatLng($address, $proxy='') {if ($address == '')return array('code'=>-1,'lat'=>0,'lng'=>0);$url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($address) . "&sensor=false";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if (!empty(self::$proxy)) {$n = rand(0, count(self::$proxy)-1);$proxy = self::$proxy[$n];curl_setopt ($ch, CURLOPT_PROXY, $proxy);}curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 35);$content = curl_exec($ch);curl_close($ch);$lat = $lng = 0;$data = json_decode($content, true);if ($data == false) {return array('code'=>-1,'lat'=>$lat,'lng'=>$lng);}$status = $data['status'];switch ($status) {case	'OK':$code = 0;break;case	'ZERO_RESULTS':$code = 1;break;case	'OVER_QUERY_LIMIT':$code = -1;break;case	'REQUEST_DENIED':$code = -2;break;case	'INVALID_REQUEST':$code = -3;break;}if (empty($data['results'])||$code!==0) {return array('code'=>$code,'lat'=>$lat,'lng'=>$lng);}$result = $data['results'][0];$geometry = $result['geometry'];if (!array_key_exists('location', $geometry)) {return array('code'=>$code,'lat'=>$lat,'lng'=>$lng);}$location = $geometry['location'];$lat = $location['lat'];$lng = $location['lng'];return array('code'=>$code,'lat'=>$lat,'lng'=>$lng);}

根据坐标查询地址

public function getAddress($lat, $lng, $proxy='') {$url = "http://ditu.google.cn/maps/api/geocode/json?latlng={$lat},{$lng}&language=zh-CN&sensor=false";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if (!empty($proxy)) {curl_setopt ($ch, CURLOPT_PROXY, $proxy);}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);$content = curl_exec($ch);curl_close($ch);$data = json_decode($content, true);if ($data == false) {return array('code'=>-1,'province'=>'','city'=>'','area'=>'');}print_r($data);$status = $data['status'];switch ($status) {case	'OK':$code = 0;break;case	'ZERO_RESULTS':$code = 1;break;case	'OVER_QUERY_LIMIT':$code = -1;break;case	'REQUEST_DENIED':$code = -2;break;case	'INVALID_REQUEST':$code = -3;break;}if (empty($data['results'])||$code!==0) {return array('code'=>$code,'province'=>'','city'=>'','area'=>'');}$province = $area = $city = '';$results = $data['results'];$address_components = $results[0]['address_components'];foreach ($address_components as $a) {$types = $a['types'];if (in_array('sublocality', $types)) {$area = $a['long_name'];} else if (in_array('administrative_area_level_1', $types)) {$province = $a['long_name'];} else if (in_array('locality', $types)) {$city = $a['long_name'];}}return array('code'=>$code, 'province'=>$province, 'city'=>$city, 'area'=>$area);}

百度地址获取

public function getAddressBaidu($lat, $lng, $proxy='') {$url = "http://api.map.baidu.com/geocoder?location={$lat},{$lng}&output=json&key=iC95iWGwGrUSadjYeZcyOIiP";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if (!empty($proxy)) {curl_setopt ($ch, CURLOPT_PROXY, $proxy);}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);$content = curl_exec($ch);curl_close($ch);$data = json_decode($content, true);if ($data == false) {return array('code'=>-1,'province'=>'','city'=>'','area'=>'');}print_r($data);$status = $data['status'];switch ($status) {case	'OK':$code = 0;break;case	'ZERO_RESULTS':$code = 1;break;case	'OVER_QUERY_LIMIT':$code = -1;break;case	'REQUEST_DENIED':$code = -2;break;case	'INVALID_REQUEST':$code = -3;break;}if (empty($data['result'])||$code!==0) {return array('code'=>$code,'province'=>'','city'=>'','area'=>'');}$province = $area = $city = '';$result = $data['result'];$address_components = $result['addressComponent'];$city = $address_components['city'];$area = $address_components['district'];$province = $address_components['province'];return array('code'=>$code, 'province'=>$province, 'city'=>$city, 'area'=>$area);}

根据经纬度查询地址

//根据经纬度查询地址public function getAddressOld($lat, $lng, $proxy='') {$url = sprintf("http://ditu.google.cn/maps/geo?output=json&key=ABQIAAAAm5e8FerSsVCrPjUC9W8BqBShYm95JTkTs6vbZ7nB48Si7EEJuhQJur9kGGJoqUiYond0w-7lKR6JpQ&q=%s,%s", $lat, $lng);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if (!empty($proxy)) {curl_setopt ($ch, CURLOPT_PROXY, $proxy);}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);$content = curl_exec($ch);curl_close($ch);$arr = json_decode($content, true);if ($arr === false) {return array('code'=>-1,'province'=>'','city'=>'','area'=>'');}$status = $arr['Status']['code'];if ($status != 200) {return array('code'=>$status,'province'=>'','city'=>'','area'=>'');}$points = $arr['Placemark'];if (count($points) == 0) {return array('code'=>$status,'province'=>'','city'=>'','area'=>'');}$country = $points[0]['AddressDetails']['Country'];$adminArea = $country['AdministrativeArea'];$province = $adminArea['AdministrativeAreaName'];$city = $adminArea['Locality']['LocalityName'];$area = $adminArea['Locality']['DependentLocality']['DependentLocalityName'];return array('code'=>$status, 'province'=>$province, 'city'=>$city, 'area'=>$area);}
public function getDataFromUrl($url){$ch = curl_init();// set URL and other appropriate options$options = array(CURLOPT_URL => $url,CURLOPT_HEADER => 0,CURLOPT_RETURNTRANSFER => 1,CURLOPT_FOLLOWLOCATION => 1,CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)',CURLOPT_TIMEOUT => 60, // 1 minute timeout (should be enough)CURLOPT_SSL_VERIFYPEER=>0,CURLOPT_SSL_VERIFYHOST=>0);curl_setopt_array($ch, $options);$ret = curl_exec($ch);curl_close($ch);return $ret;}

根据谷歌座标转换百度座标

//根据谷歌座标转换百度座标public static function convertGoogle($lat, $lng, $from=2, $to=4) {$url = "http://api.map.baidu.com/ag/coord/convert?from={$from}&to={$to}&x={$lng}&y={$lat}";$ret = self::getDataFromUrl($url);if ($ret === false)return false;$arr = json_decode($ret, true);if ($arr == false)return false;$err = $arr['error'];if ($err != 0) return false;	$lng = base64_decode($arr['x']);$lat = base64_decode($arr['y']);return array($lat, $lng);}

通过IP获取地址

public static function getAddressByIp($ipaddr) {$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ipaddr";$content = Utility::getDataFromUrl($url);$arr = json_decode($content, true);if ($arr == false) {return false;}return $arr;}

相关文章:

  • C++对象模型(一)
  • Linux平台下安全编译
  • Git搭建
  • 通过FileZilla配置FTP
  • gitlab.rb主要配置
  • linux install nvm
  • 如何令containerd连接私有harbor
  • SQL基础知识(四)
  • Golang内置类型和函数及接口、Init函数和main函数
  • 无状态应用管理Deployment
  • 面试经典题---3.无重复字符的最长子串
  • php二次开发股票系统代码:腾讯股票数据接口地址、批量获取股票信息、转换为腾讯接口指定的股票格式
  • 幻兽帕鲁服务器数据备份
  • x-cmd pkg | httpx - 为 Python 设计的下一代 HTTP 客户端库
  • 04 SB实战 -微头条之头条模块(登录验证拦截器+发布文章+修改文章)
  • angular2开源库收集
  • JDK 6和JDK 7中的substring()方法
  • nodejs实现webservice问题总结
  • Redis学习笔记 - pipline(流水线、管道)
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 关于字符编码你应该知道的事情
  • 基于游标的分页接口实现
  • 技术发展面试
  • 理解在java “”i=i++;”所发生的事情
  • 聊聊flink的BlobWriter
  • 模型微调
  • 你真的知道 == 和 equals 的区别吗?
  • 前端临床手札——文件上传
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • %@ page import=%的用法
  • (zhuan) 一些RL的文献(及笔记)
  • (定时器/计数器)中断系统(详解与使用)
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (简单) HDU 2612 Find a way,BFS。
  • (力扣题库)跳跃游戏II(c++)
  • (三十五)大数据实战——Superset可视化平台搭建
  • (四)模仿学习-完成后台管理页面查询
  • (一)插入排序
  • .net/c# memcached 获取所有缓存键(keys)
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .考试倒计时43天!来提分啦!
  • @软考考生,这份软考高分攻略你须知道
  • [ 常用工具篇 ] AntSword 蚁剑安装及使用详解
  • [acwing周赛复盘] 第 69 场周赛20220917
  • [BeginCTF]真龙之力
  • [bug总结]: Feign调用GET请求找不到请求体实体类
  • [BZOJ 3531][Sdoi2014]旅行(树链剖分+线段树)
  • [C++]命名空间等——喵喵要吃C嘎嘎
  • [codevs 1296] 营业额统计