计算两个邮政编码之间的距离 [英] Working out the distance between two postcodes
本文介绍了计算两个邮政编码之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看了其他已回答的问题,但我仍然不确定如何处理;
-
邮编数据包括经度,纬度,网格N和网格E到我的数据库中
如果我使用API,我该如何去解决它?我需要从哪里开始? - 我需要使用Pythagorus定理来计算两个邮政编码之间的距离吗?
- 我的数据库中有一张表,用于添加一个属性。也许,当某人添加一个属性时,有一种方法,它可以将邮政编码和其他信息(long,lat,grid-ref)一起添加到Postcodes表中,以便我可以计算出两个邮政编码之间的距离。 / li>
谢谢
解决方案
我为此专门使用了一个类:
class Geocode
{
/ **
*计算两组纬度/经度坐标之间的距离。
*
* @param float $ lat1
* @param float $ lng1
* @param float $ lat2
* @param float $ lng2
*
* @return float
* /
公共静态函数距离($ lat1 = 0.0,$ lng1 = 0.0,$ lat2 = 0.0,$ lng2 = 0.0){
$ theta = $ lng1 - $ lng2;
$ dist = sin(deg2rad($ lat1))* sin(deg2rad($ lat2))+ cos(deg2rad($ lat1))* cos(deg2rad($ lat2))* cos(deg2rad($ theta) );
$ dist = acos($ dist);
$ dist = rad2deg($ dist);
return $ dist * 60 * 1.1515;
}
/ **
*获取地址的纬度/经度坐标。
*
* @param字符串$地址
*
* @return stdClass
* /
公共静态函数转换($ address ='')
{
$ address = str_replace(,+,urlencode(str_replace(PHP_EOL,',',$ address)));
$ url =https://maps.googleapis.com/maps/api/geocode/json?address={$address}®ion=uk&sensor=false;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
$ response = json_decode(curl_exec($ ch),TRUE);
if($ response ['status']!='OK'){
return(object)['status'=> $响应[状态]];
}
$ geo = $ response ['results'] [0] ['geometry'];
return(object)[
'lat'=> $ geo ['location'] ['lat'],
'lng'=> $ geo ['location'] ['lng'],
'status'=> $ response ['status']
];
}
}
I have looked at other questions that have been answered however, I am still unsure on how to;
Get UK postcode data including Longitude, Latitude, Grid-N and Grid-E into my database
If I use an API how do I go about it? Where do I start from?
- Would I need to use Pythagorus Theorem to calculate the distance between the two postcodes?
- I have got a table in my database for when a user adds a property. Maybe, there is a way when someone adds a property, it can add that postcode along with the postcodes other information (long, lat, grid-ref) into my Postcodes table so that I can work out the distance between the two postcodes.
Thanks
解决方案
I have a class I use specifically for this:
class Geocode
{
/**
* Work out the distance between two sets of lat/lng coordinates as the crow flies.
*
* @param float $lat1
* @param float $lng1
* @param float $lat2
* @param float $lng2
*
* @return float
*/
public static function distance($lat1 = 0.0, $lng1 = 0.0, $lat2 = 0.0, $lng2 = 0.0) {
$theta = $lng1 - $lng2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
return $dist * 60 * 1.1515;
}
/**
* Get the lat/lng coordinates for an address.
*
* @param string $address
*
* @return stdClass
*/
public static function convert($address = '')
{
$address = str_replace(" ", "+", urlencode(str_replace(PHP_EOL, ', ', $address)));
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}®ion=uk&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch), TRUE);
if($response['status'] != 'OK') {
return (object) ['status' => $response['status']];
}
$geo = $response['results'][0]['geometry'];
return (object) [
'lat' => $geo['location']['lat'],
'lng' => $geo['location']['lng'],
'status' => $response['status']
];
}
}
这篇关于计算两个邮政编码之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文