Reputation: 111
If I have a class that defines a map with the top/left defined by a longitude and latitude, and the bottom/right also defined by a longitude and latitude, how can test if a given latitude/longitude is within the map's bounding points? {This is not a question that has to do with Google Maps). eg (is Orlando within a map that covers Tallhasse to Miami).
public class MapContext {
private Location mMapTop = null;
private Location mMapBottom = null;
public MapContext(String topLatitude,String topLongitude, String bottomLatitude, String bottomLongitude) {
double theTopLat = Location.convert(topLatitude);
double theTopLong = Location.convert(topLongitude);
mMapTop = new Location("private");
mMapTop.setLongitude(theTopLong);
mMapTop.setLatitude(theTopLat);
double theBottomLat = Location.convert(bottomLatitude);
double theBottomLong = Location.convert(bottomLongitude);
mMapBottom = new Location("private");
mMapBottom.setLongitude(theBottomLong);
mMapBottom.setLatitude(theBottomLat);
} public boolean testIfPointOnMap(Location location) { ? ? return TRUE or FALSE } }
Upvotes: 6
Views: 7374
Reputation: 12196
Can you just check to see if the lat long is between the bounds?
/*
* top: north latitude of bounding box.
* left: left longitude of bounding box (western bound).
* bottom: south latitude of the bounding box.
* right: right longitude of bounding box (eastern bound).
* latitude: latitude of the point to check.
* longitude: longitude of the point to check.
*/
boolean isBounded(double top, double left,
double bottom, double right,
double latitude, double longitude){
/* Check latitude bounds first. */
if(top >= latitude && latitude >= bottom){
/* If your bounding box doesn't wrap
the date line the value
must be between the bounds.
If your bounding box does wrap the
date line it only needs to be
higher than the left bound or
lower than the right bound. */
if(left <= right && left <= longitude && longitude <= right){
return true;
} else if(left > right && (left <= longitude || longitude <= right)) {
return true;
}
}
return false;
}
Upvotes: 11
Reputation: 40889
Here is a complete Java class for specifying a bounding box and checking if a point lies inside of it. The box is defined by its southwest and northeast geocoordinates (latitude and longitude).
class Bbox
{
public double swLatitude = 0.0;
public double swLongitude = 0.0;
public double neLatitude = 0.0;
public double neLongitude = 0.0;
/*************************************************************************
Constructor.
@param bboxSpecification A comma-separated string containing the
southwest latitude, soutwest longitude, northest latitude, and
northest longitude.
*************************************************************************/
public Bbox(String bboxSpecification)
{
String tokens[] = bboxSpecification.split("(?:,\\s*)+");
if (tokens.length != 4)
{
throw new IllegalArgumentException(
String.format("Expected 4 values in bbox string but found %d: %s\n",
tokens.length, bboxSpecification));
}
swLatitude = Double.parseDouble(tokens[0]);
swLongitude = Double.parseDouble(tokens[1]);
neLatitude = Double.parseDouble(tokens[2]);
neLongitude = Double.parseDouble(tokens[3]);
}
@Override
public String toString()
{
return String.format("swLatitude=%f, swLongitude=%f, neLatitude=%f, neLongitude=%f",
swLatitude, swLongitude, neLatitude, neLongitude);
}
/*************************************************************************
Checks if the bounding box contains the latitude and longitude. Note that
the function works if the box contains the prime merdian but does not
work if it contains one of the poles.
*************************************************************************/
public boolean contains(double latitude, double longitude)
{
boolean longitudeContained = false;
boolean latitudeContained = false;
// Check if the bbox contains the prime meridian (longitude 0.0).
if (swLongitude < neLongitude)
{
if (swLongitude < longitude && longitude < neLongitude)
{
longitudeContained = true;
}
}
else
{
// Contains prime meridian.
if ((0 < longitude && longitude < neLongitude) ||
(swLongitude < longitude && longitude < 0))
{
longitudeContained = true;
}
}
if (swLatitude < neLatitude)
{
if (swLatitude < latitude && latitude < neLatitude)
{
latitudeContained = true;
}
}
else
{
// The poles. Don't care.
}
return (longitudeContained && latitudeContained);
}
public static void test()
{
Bbox bbox;
double latitude = 0;
double longitude = 0;
bbox = new Bbox("37.43, -122.38, 37.89, -121.98");
latitude = 37.5;
longitude = -122.0;
System.out.printf("bbox (%s) contains %f, %f: %s\n",
bbox, latitude, longitude, bbox.contains(latitude, longitude));
bbox = new Bbox("50.99, -2.0, 54, 1.0");
latitude = 51.0;
longitude = 0.1;
System.out.printf("bbox (%s) contains %f, %f: %s\n",
bbox, latitude, longitude, bbox.contains(latitude, longitude));
}
}
Upvotes: 1
Reputation: 111
Did not find a perfect solution, but using simple bounds checking is okay, as the zoom level gets more percise the variance in the position becomes less and less pronounced. Not as percise as I want it but within limits of GPS accuracy.
Upvotes: 0
Reputation: 28687
Please post your code - but assuming you have something like this:
public class Map{
public int x1, y1, x2, y2;
}
Your check would be something simple as this:
boolean isPointInMap(Map m, int x, int y){
return m.x1 <= x && x <= m.x2 && m.y1 <= y && y <= m.y2;
}
Upvotes: 1