2011-12-17 9 views
4

緯度と経度で定義された上/左、下/右でも定義されたマップを定義するクラスがある場合経度と緯度、与えられた緯度/経度がマップの境界点内にあるかどうかをテストするにはどうすればよいですか? {これはGoogleマップと関係のある質問ではありません。 (例:Tallhasse to Miamiをカバーする地図内のオーランドです)。緯度/経度ポイントが地図内にあるかどうかをテストする方法(Googleマップではない)

パブリッククラス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

あなたの質問に(2つのバウンディングポイントで)言い方をすると、あなたの「マップ」は単純な行に制限されます。 2つ以上のディメンションを持つリージョンを指定する場合は、少なくとも3つのポイント(オルランド、シカゴ、ニューヨークなど)が必要です。 –

+0

@Tom私は彼がバウンディングボックスについて話していると仮定しました。 – Dev

+0

あなたが求めているのは、実際にポイントが四角形の中にあるかどうかを調べる方法です。これは、このサイトや他のすべての場所で、何回もカバーされています。 – Thor84no

答えて

4

緯度経度が範囲内にあるかどうかを確認できますか?

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

tlat = top lat、llong = left lng、blat = bottom lat、rlong = right lng、xlat =ポイントx lat、xlong =ポイントx lng、正しい? – HighFlyingFantasy

+0

@HighFlyingFantasy正しいと、実際に私はちょうど修正した私の例でエラーを発見した。 – Dev

+2

変数とは何ですか?貴重な30秒を費やし、分かりやすい変数名を書くことができます。 – stackoverflowuser2010

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; 
} 
+1

マップの境界に依存します。私は同様の問題に直面しています。地図X1、X2(経度)は-5.525、-135.00です。完全に有効な組み合わせ。この場合、緯度/経度座標は境界内にありません。 -135は+225 –

+0

@ Dr.ABTに翻訳する必要があります - 合意、ありがとう! – ziesemer

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)); 
    } 
} 
関連する問題