2011-12-17 81 views
4

如果我有一個類定義了一個由經度和緯度定義的頂部/左邊的地圖,並且底部/右邊也由經度和緯度,如何測試給定的經度/緯度是否在地圖的邊界點內? {這不是一個與Google地圖有關的問題)。 例如(奧蘭多在一張覆蓋Tallhasse到邁阿密的地圖中)。如何測試緯度/經度點是否在一個地圖內(不是谷歌地圖)

公共類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); 

} 公共布爾testIfPointOnMap(位置定位){ ? ? 返回TRUE或FALSE } }

+0

注意到@下面zeisemer的答案,你的方式措辭你的問題(有兩個邊界點),一種限制你的「地圖」,以作爲一個簡單的線條。如果你想指定一個多維度的區域,那麼你至少需要三點,比如奧蘭多,芝加哥和紐約。 – 2011-12-17 03:20:14

+0

@Tom我以爲他在談論一個邊框 – Dev 2011-12-17 03:22:47

+0

你問的只是如何找出一個點是否在矩形內。這已經被覆蓋了很多次,遍及這個網站和其他網站。 – Thor84no 2011-12-17 03:28:51

回答

4

您可以檢查lat long是否在邊界之間嗎?

/* 
    * 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; 
    } 
1

請發表您的代碼 - 但假設你有這樣的事情:

public class Map{ 
    public int x1, y1, x2, y2; 
} 

你的檢查會因爲這簡單的東西:

boolean isPointInMap(Map m, int x, int y){ 
    return m.x1 <= x && x <= m.x2 && m.y1 <= y && y <= m.y2; 
} 
0

沒有找到一個完美的解決方案,但使用簡單的邊界檢查是可以的,因爲縮放級別變得更加靈活,位置的變化變得越來越不明顯。不像我想要的那樣精確,但在GPS準確度的範圍內。

0

這是一個完整的Java類,用於指定邊界框並檢查點是否位於其中。該框由其西南和東北地理座標(經度和緯度)定義。

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)); 
    } 
} 
相關問題