I have a location in longitude/latitude (x,y)
and a distance d
. Now I want to find the maximum and minimum longitude and latitude that are within the the distance d
from my location. The goal is to create a box with exact width and height, with the location (x,y)
in the middle.
I have found code for getting the distance from two points, and through this I could search for the correct longitude/latitude by getting the distance between points that only differs in longitude or latitude until I find the correct distance. But this is obviously not the best solution, and the goal of this is to get a solution that is very fast when implemented in code. However, if it helps, here is the implementation that gives the distance between two locations (in java):
public static double getDistanceMeter(double currentLongitude, double currentLatitude, double goalLongitude, double goalLatitude) { double meterConversion = 1609; double R = 3958.7558657440545D; // Radius of the earth in km double dLat = Math.toRadians(goalLatitude - currentLatitude); double dLon = Math.toRadians(goalLongitude - currentLongitude); double a = Math.sin(dLat / 2D) * Math.sin(dLat / 2D) + Math.cos(Math.toRadians(currentLatitude)) * Math.cos(Math.toRadians(goalLatitude)) * Math.sin(dLon / 2D) * Math.sin(dLon / 2D); double c = 2D * Math.atan2(Math.sqrt(a), Math.sqrt(1D - a)); double d = R * c * meterConversion; // Distance in m return d; }
(if someone would help me find better tags for this question, I would really appreciate it, I feel lost in the math-lingo)