计算“像乌鸦一样”距离PHP [英] Calculate "as the crow flies" distance php

查看:77
本文介绍了计算“像乌鸦一样”距离PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在计算一个函数内的一个wordpress网站上两点之间的距离。我通过调用

I am currently calculating the driving distance between two points on one of my wordpress websites inside of a function. I accomplish this using the google distance matrix by calling

wp_remote_get(
    "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".
    urlencode($origin).
    "&destinations=".
    urlencode($destination).
    "&sensor=false&units=imperial"
)

然后插入原点和目的地用户通过表单输入到网址中。是否有可能使用类似的方法来计算像乌鸦一样的距离,或者是否必须重做我的函数?

and then inserting the origins and destinations users have entered via a form into the url. Is it possible to use a similar approach to calculating an "as the crow flies" distance or do I have to rework my function?

推荐答案

< (Lat1,lon1)到(lat2,lon2)

Distance between 2 points: (lat1,lon1) to (lat2,lon2)

distance = acos(
     cos(lat1 * (PI()/180)) *
     cos(lon1 * (PI()/180)) *
     cos(lat2 * (PI()/180)) *
     cos(lon2 * (PI()/180))
     +
     cos(lat1 * (PI()/180)) *
     sin(lon1 * (PI()/180)) *
     cos(lat2 * (PI()/180)) *
     sin(lon2 * (PI()/180))
     +
     sin(lat1 * (PI()/180)) *
     sin(lat2 * (PI()/180))
    ) * 3959






3959是以英里为单位的地球半径。用KM中的
半径替换此值(或任何其他单位),以获得同一单元的结果。


3959 is the Earth radius in Miles. Replace this value with radius in KM, (or any other unit), to get results on the same unit.

您可以通过比较来验证您的实施此 工作示例

You can verify your implementation by comparing to this worked example:

这篇关于计算“像乌鸦一样”距离PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆