Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a bounding box around the geo point

Tags:

android

gps

I have a geo latitude and longitude (ex: 39.6199,-79.9535). How can I build a bounding box in Java around the point with 1km of radius?

like image 463
ChanChow Avatar asked Dec 07 '22 12:12

ChanChow


2 Answers

  1. The distance between two longitude lines changes according to the latitude line you're on. It can be computed as:

    3960 * 2 * pi /360 * cosine(latitude) in miles

  2. The distance between two latitude lines is constant everywhere: 69 miles.

So, in order to draw a square of 1x1 miles around a geographical location you should find the two latitude lines parallel to the latitude of the point with distance 0.5 mile along south and north. Then find two parallel longitude lines with 0.5 miles distance along west and east.

For example, 0.5 mile means 0.5/69 latitude difference. If the latitude of the given point is 39.6199 then the latitudes of upper and lower sides of the square have latitude values: 36.6199+(0.5/69) and 36.6199-(0.5/69) respectively.

like image 121
mostar Avatar answered Dec 23 '22 03:12

mostar


double latitude = location.getLatitude();
double longitude = location.getLongitude();

// 6378000 Size of the Earth (in meters)
double longitudeD = (Math.asin(1000 / (6378000 * Math.cos(Math.PI*latitude/180))))*180/Math.PI;
double latitudeD = (Math.asin((double)1000 / (double)6378000))*180/Math.PI;

double latitudeMax = latitude+(latitudeD);
double latitudeMin = latitude-(latitudeD);
double longitudeMax = longitude+(longitudeD);
double longitudeMin = longitude-(longitudeD);
like image 23
AlvaroSantisteban Avatar answered Dec 23 '22 02:12

AlvaroSantisteban