I'm trying to get a bounding box coordinates in javascript from just 1 coordinate (latitude,longitude), then I could use it as viewbox in the OSM Nominatin API.
For the red point, how to get the green points for any area/country in the planet? (Raw difference of 0.15 for example):
for (x,y) will be: (x-2,y+2), (x+2,y-2)?
function get_bounce(lat, lng) { // y,x
// x-0.15,y+0.15
// x,y
// x+0.15,y-0.15
var lng1 = lng - 0.15;
var lat1 = lat + 0.15;
var lng2 = lng + 0.15;
var lat2 = lat - 0.15;
return [lat1,lng1,lat2,lng2];
}
Thanks in advance!
The 1 of degree longitude distance varies as was said in a comment depending on the latitude you are at. Longitude is wider at the equator and narrows down at the poles, unlike latitude that has a constant size across the globe. To visualize this see:
Visualize the globe
Another good reference that helped me was:
Calculate distance, bearing and more between Latitude/Longitude points
I had a similar problem. Here is the code I came up with to get a pretty good bounding box. Give this function a gps location string ("x.xx, y.yy") and "radius" in km and it generates the north, south, east, west decimal coordinates for a bounding box. This is in Java but should be easy to convert to another language.
The above code give a pretty good approximation that can be off a mile or two depending on your latitude. If you want to be more accurate then you might take a look at the answers here:
Geotools: bounding box for a buffer in wgs84 See Eduardo's answer to this question. It seems like a pretty accurate solution.