繁体   English   中英

如何使用Java检查所选区域的纬度/经度是否在数据库中定义的范围内

[英]How to check if the lat/lon of a selected area lies within the range defined in a database using Java

我有一个使用OpenLayers渲染地图的地图。 通过在地图上选择一个区域,我们可以找到所选区域的纬度/经度值。 我在定义特定车辆纬度/经度值的数据库中有数据。 我想检查车辆是否在我在地图上选择的区域内。 我将如何用Java做到这一点?

(function () {
    var map, drawControls, boxLayer;

    function init() {
        map = new OpenLayers.Map('map');

        var wmsLayer = new OpenLayers.Layer.WMS("OpenLayers WMS",
            "http://vmap0.tiles.osgeo.org/wms/vmap0?", {
            layers: 'basic'
        });


        boxLayer = new OpenLayers.Layer.Vector("Box layer");

        map.addLayers([wmsLayer, boxLayer]);
        map.addControl(new OpenLayers.Control.LayerSwitcher());
        map.addControl(new OpenLayers.Control.MousePosition());

        var box = new OpenLayers.Control.DrawFeature(boxLayer,
        OpenLayers.Handler.RegularPolygon, {
            handlerOptions: {
                sides: 4,
                irregular: true
            },

            callbacks: {
                up: function (geom) {
                    console.log(geom);
                    alert(geom);
                }
            }
        });

        map.addControl(box);
        box.activate();


        map.setCenter(new OpenLayers.LonLat(0, 0), 3);

    }

    init();

})();

Jsfiddle选择地图上的区域

上面的小提琴在地图上选择了一个区域,我有一个Postgres数据库,其中有一个表track_vehicle,其列vehicle_id,纬度和经度。 我想检查车辆的纬度和经度是否在使用Java在地图上选择的范围内。

纬度和经度是数字(正数表示东/南,负数表示西/北),因此您可以使用大于和小于操作来检查点是否位于区域内。

因此66° 33′ 39″ N 66033039变为6603303966° 33′ 39″ S 66033039 66° 33′ 39″ S变为-66033039 只需将每个步骤写成三位数的整数,您总会以九位数结尾。

public int degreesToInt(int degrees, int minutes, int seconds) {
    NumberFormat nf = new DecimalFormat("000");
    return Integer.valueOf(nf.format(degrees) +
                           nf.format(minutes) +
                           nf.format(seconds));
}

public boolean objectInArea(int vehicleLatitude, int vehicleLongitude,
                            int northLatitudeBorder, int southLatitudeBorder,
                            int westLongitudeBorder, int eastLongitudeBorder) {
    if(westLongitudeBorder > eastLongitudeBorder) {
       return objectInArea(vehicleLatitude, vehicleLongitude,
                           northLatitudeBorder, southLatitudeBorder, 
                           -180000000,eastLongitudeBorder) ||
              objectInArea(vehicleLatitude, vehicleLongitude,
                           northLatitudeBorder, southLatitudeBorder, 
                           westLongitudeBorder,180000000);
    }
    return vehicleLongitude < eastLongitudeBorder &&
           vehicleLongitude > westLongitudeBorder &&
           vehicleLatitude < southLatitudeBorder &&
           vehicleLatitude > northLatitudeBorder;
}

您可能会问:“为什么是三位数而不是两位?”

  1. 它没有缺点。
  2. 您可以更轻松地读取数字,因为分钟和秒永远不会有数百位,因此总会有一个零,这使调试更加容易。
  3. 您还可以将相同的度数转换为度数(东/西度超过100度),也可以转换为分钟和秒。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM