PHP根据两点间的经纬度计算距离

yipeiwu_com5年前PHP代码库

这是一个不错的示例,直接贴代码,首先要知道纬度值、经度值

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

/* 
Convert these degrees to radians 
to work with the formula 
*/ 

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

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

/* 
Using the 
Haversine formula 

http://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); 
} 

相关文章

解决form中action属性后面?传递参数 获取不到的问题

如下所示: $p_id = $_REQUEST['p_id']; echo "<h1>您将更新商品编号为<span>$p_id</span>的商...

PHP 读取文件内容代码(txt,js等)

<?php /* 作者:bjf; 应用:读取文件内容; */ function read_file_content($FileName) { //open file $fp=fop...

php过滤所有恶意字符(批量过滤post,get敏感数据)

函数代码:复制代码 代码如下://php 批量过滤post,get敏感数据 if (get_magic_quotes_gpc()) { $_GET = stripslashes_arra...

采集邮箱的php代码(抓取网页中的邮箱地址)

复制代码 代码如下: <?php $url='//www.jb51.net'; //这个网页里绝对含有邮件地址。 $content=file_get_contents($url);...

PHP命名空间简单用法示例

本文实例讲述了PHP命名空间简单用法。分享给大家供大家参考,具体如下: 有三个文件,分别是space1.php,space2.php,use.php吧,在同一目录里。 space1.ph...