检查一个城市是否位于东北纬度/伦敦和西南纬度/伦敦的国家

时间:2014-03-02 16:08:00

标签: php json google-maps geometry

我正在使用谷歌API的PHP应用程序,我想检查一个城市是否在一个国家。

例如:法国是巴黎吗?

法国几何界限:

 "northeast" : {
                  "lat" : 51.089166,
                  "lng" : 9.560067799999999
               },
 "southwest" : {
                  "lat" : 41.3423276,
                  "lng" : -5.141227900000001
               }

巴黎几何界限:

 "northeast" : {
              "lat" : 48.9021449,
              "lng" : 2.4699208
           },
 "southwest" : {
              "lat" : 48.815573,
              "lng" : 2.224199
           }

感谢。

1 个答案:

答案 0 :(得分:1)

有趣的问题。看看这个我做了一个例子:

function inRange($value, $max, $min) {
   return $value >= $min && $value <= $max;
}

$city_coords_northeast = // put your northeast city coords here(array)
$city_coords_southwest = // put your southwest city coords here(array)

$country_coords_northeast = // put your northeast country coords here(array)
$country_coords_southwest = // put your southwest country coords here(array)

if( inRange($city_coords_northeast['lat'], $country_coords_northeast['lat'], $country_coords_southwest['lat']) && 
    inRange($city_coords_northeast['long'], $country_coords_northeast['long'], $country_coords_southwest['long']) &&
    inRange($country_coords_southwest['lat'], $city_coords_northeast['lat'], $city_coords_southwest['lat']) &&
    inRange($country_coords_southwest['long'], $city_coords_northeast['long'], $city_coords_southwest['long']) ) {
   echo 'Paris is in France!';
}
PS:我认为这不是很快,但是它起了作用。