【问题标题】:How to test if a Latitude/Longitude point is within a Map (not google maps)如何测试纬度/经度点是否在地图内(不是谷歌地图)
【发布时间】:2012-01-22 10:58:28
【问题描述】:

如果我有一个定义地图的类,其顶部/左侧由经度和纬度定义,底部/右侧也由经度和纬度定义,如何测试给定的纬度/经度是否在地图的范围内边界点? {这与谷歌地图无关)。 例如(在从 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 } }

【问题讨论】:

  • 注意到下面@zeisemer 的回答,您提出问题的方式(带有两个边界点)将您的“地图”限制为一条简单的线。如果要指定一个多维的区域,则至少需要三个点,例如奥兰多、芝加哥和纽约。
  • @Tom 我以为他说的是边界框
  • 您要问的实际上只是如何找出一个点是否在矩形内。这已在本网站和其他网站上多次介绍过。

标签: java android


【解决方案1】:

你能检查一下经纬度是否在界限之间吗?

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

【讨论】:

  • tlat = 上纬度,llong = 左经度,blat = 下纬度,rlong = 右经度,xlat = 点 x 纬度,xlong = 点 x lng,对吗?
  • @HighFlyingFantasy 正确,实际上在审查后我发现我刚刚纠正的示例中有一个错误。
  • 变量是什么?你可以花宝贵的 30 秒,写出易于理解的变量名。
  • @stackoverflowuser2010 澄清变量名称。
【解决方案2】:

请发布您的代码 - 但假设您有这样的东西:

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

【讨论】:

  • 取决于地图边界。我遇到了类似的问题,地图 X1、X2(经度)为 -5.525、-135.00。一个完全有效的组合。在这种情况下,范围内没有纬度/经度坐标。 -135 需要翻译成 +225
【解决方案3】:

这是一个完整的 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));
    }
}

【讨论】:

    【解决方案4】:

    没有找到完美的解决方案,但使用简单的边界检查是可以的,因为缩放级别越来越精确,位置的变化变得越来越不明显。没有我想要的那么精确,但在 GPS 精度范围内。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      相关资源
      最近更新 更多