简体   繁体   English

如何测试纬度/经度点是否在地图内(不是谷歌地图)

[英]How to test if a Latitude/Longitude point is within a Map (not google maps)

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). {这不是与Google地图有关的问题)。 eg (is Orlando within a map that covers Tallhasse to Miami). 例如(奥兰多在地图上覆盖Tallhasse到迈阿密)。

public class MapContext { 公共类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) { ? public boolean testIfPointOnMap(Location location){? ? return TRUE or FALSE } } 返回TRUE或FALSE}}

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

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

Here is a complete Java class for specifying a bounding box and checking if a point lies inside of it. 这是一个完整的Java类,用于指定边界框并检查点是否位于其中。 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));
    }
}

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. 不像我想要的那样精确,但在GPS精度范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中获取谷歌地图蓝点的经度和纬度 - how to get the google map blue point latitude and longitude in android 谷歌地图不返回经度和纬度 - Google maps not returning longitude and latitude 如何从给定点(经度,纬度)计算给定半径内的所有点(经度,纬度)? - how to calculate all points(longitude,latitude) within a given radius from given point (longitude,latitude)? 转换谷歌地图中的纬度和经度? - converting latitude and longitude in google map? 如何将关键字转换为经度谷歌地图[Android] - How to convert keyword into Latitude Longitude Google Maps [Android] 如何从谷歌地图 URL 获取纬度经度 - How to get latitude longitude from google map URL 如何通过纬度和经度在谷歌地图上显示多个位置? - how to display multiple location on google map by their latitude and longitude? 如何在Google Map v2中获取标记的经度和纬度值 - How to get the longitude and latitude value of the marker in google map v2 当网络服务中的经度和纬度发生变化时,如何在Google地图中移动标记? - How to move the marker in Google map when the latitude and longitude changes in webservices? 如何在android studio中的谷歌地图上获取选定位置的纬度和经度 - How to get latitude and longitude of select location on google map in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM