2012-12-12 64 views
6

我目前正在计算一个函数内的一个wordpress网站上两点之间的行驶距离。我做到这一点使用谷歌距离矩阵通过调用计算“作为乌鸦”的距离php

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

,然后插入起始点和目的地用户已经通过形式进入网址输入。是否有可能使用类似的方法来计算“像乌鸦一样”的距离,还是必须重新修改我的功能?

+1

,你可以没有谷歌地图做。搜索让您获得地球上两点之间物理最短距离的公式。 – MAXIM

+0

此问题与Google Maps API V3无关。 (标签已移除) – Marcelo

回答

7

2点之间的距离:至(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(LAT1,lon1)是在万里地球半径。用KM(或任何其他单位)中的 半径替换此值,以获得同一单元上的结果。

您可以通过比较这worked example验证的实现: