我正在计算一个函数内部的一个wordpress网站上两点之间的行车距离.我通过调用谷歌距离矩阵完成此任务
wp_remote_get(
"http://maps.googleapis.com/maps/api/distancematrix/json?origins=".
urlencode($origin).
"&destinations=".
urlencode($destination).
"&sensor=false&units=imperial"
)
Run Code Online (Sandbox Code Playgroud)
然后将用户通过表单输入的来源和目的地插入到网址中.是否有可能使用类似的方法来计算"像乌鸦飞行"的距离,还是我必须重新修改我的功能?
2点之间的距离:(lat1,lon1)到(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
Run Code Online (Sandbox Code Playgroud)
3959是英里的地球半径.将此值替换为KM(或任何其他单位)中的半径,以在同一单位上获得结果.
您可以通过与此工作示例进行比较来验证您的实现: