Reputation: 355
I'm working on a PHP application which uses google API's, and i would like to check if a city is in a country.
for example : is Paris in France ?
France geometry bounds :
"northeast" : {
"lat" : 51.089166,
"lng" : 9.560067799999999
},
"southwest" : {
"lat" : 41.3423276,
"lng" : -5.141227900000001
}
Paris geometry bounds :
"northeast" : {
"lat" : 48.9021449,
"lng" : 2.4699208
},
"southwest" : {
"lat" : 48.815573,
"lng" : 2.224199
}
Thanks.
Upvotes: 1
Views: 318
Reputation: 5235
Interesting question. Check this out I made an example:
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: I don't think this is very fast, but it makes its job.
Upvotes: 1