【问题标题】:Android GoogleMaps , How To Check Point (Latitude and Longitude) Inside a PolygonAndroid GoogleMaps,如何检查多边形内的点(纬度和经度)
【发布时间】:2013-10-31 16:01:54
【问题描述】:

关于多边形内的纬度和经度存在一些问题

但我的问题仍然没有得到任何答案

这是我的问题:

假设我们有一个看起来像矩形的多边形

(在实际问题中,它的形状不会像矩形一样,它可能有超过 2 个点,最多可能有 100 个点)

在哪里

-the top left point is combination of latitude = 100.214525 and longitude = 102.12866735
-the top right point is combination of latitude = 105.21335 and longitude = 101.12882515
-the bottom left point is combination of latitude = 29.214124 and longitude = 20.16873
-the bottom right point is combination of latitude = 30.216125 and longitude = 18.1286860

(所有经纬度都只有一个例子)

现在我有 4 个点,应该有一个具有 4 个点的多边形,并尝试从一个点到另一个点画一条线 (多边形内不会有任何洞)

现在假设,我有 2 个用户

我们说

Latitude 78.25151 and Longitude 67.2121021 is the User A's coordinate

和

Latitude 28.25151 and Longitude 157.2121021 is the User B's coordinate

我需要一些算法或可以确定用户 A / B 在多边形内的坐标

(如果用户的坐标在多边形内,则返回 true,否则返回 false 而不向用户显示任何内容)

仅供参考

我用 javascript 研究过它

这是我的 javascript

if (!google.maps.Polygon.prototype.getBounds) {
            google.maps.Polygon.prototype.getBounds = function(latLng) {
                var bounds = new google.maps.LatLngBounds();
                var paths = this.getPaths();
                var path;

                for (var p = 0; p < paths.getLength(); p++) {
                    path = paths.getAt(p);
                    for (var i = 0; i < path.getLength(); i++) {
                        bounds.extend(path.getAt(i));
                    }
                }

                return bounds;
            }
        }   



        google.maps.Polygon.prototype.containsLatLng = function(latLng) {

            var lat, lng;

            if(arguments.length == 2) {
                if(typeof arguments[0]=="number" && typeof arguments[1]=="number") {
                    lat = arguments[0];
                    lng = arguments[1];
                }
            } 
            else if (arguments.length == 1) {
                var bounds = this.getBounds();

                if(bounds != null && !bounds.contains(latLng)) {
                    return false;
                }
                lat = latLng.lat();
                lng = latLng.lng();
            } 
            else {
                console.log("Wrong number of inputs in google.maps.Polygon.prototype.contains.LatLng");
            }

            // Raycast point in polygon method
            var inPoly = false;

            var numPaths = this.getPaths().getLength();
            for(var p = 0; p < numPaths; p++) {
                var path = this.getPaths().getAt(p);
                var numPoints = path.getLength();
                var j = numPoints-1;

                for(var i=0; i < numPoints; i++) { 
                    var vertex1 = path.getAt(i);
                    var vertex2 = path.getAt(j);

                    if (vertex1.lng() < lng && vertex2.lng() >= lng || vertex2.lng() < lng && vertex1.lng() >= lng) {
                        if (vertex1.lat() + (lng - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < lat) {
                            inPoly = !inPoly;
                        }
                    }

                    j = i;
                }
            }

            return inPoly;
        }

如你所见

该代码包含 2 个函数(包含 LatLng 和 getBounds) 并且有了这些功能,如果我的纬度和经度在多边形内,我可以很容易地得到布尔值

喜欢这个例子:

var triangleCoords = [
  new google.maps.LatLng(25.774252, -80.190262),
  new google.maps.LatLng(18.466465, -66.118292),
  new google.maps.LatLng(32.321384, -64.75737) 
];

var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
var myLatitudeLongitude = new google.maps.LatLng(24.886436490787712, -70.2685546875);

alert(bermudaTriangle.containsLatLng(myLatitudeLongitude));

但那是javascript

现在在 android 中,我不知道是否有像 google.maps.Polygon 或 google.maps.LatLngBounds 这样的东西,你可以看到这两个我的 javascript 函数需要这些文件

或者有没有其他方法可以在不依赖这两个 javascript 函数的情况下做这样的事情?

【问题讨论】:

    标签: javascript android google-maps google-maps-api-3 point-in-polygon


    【解决方案1】:

    您提出的问题通常称为多边形中的点算法。
    您应该向我们说明在哪里您想实现它。在视图中、服务器端还是在您的数据库中?

    但是,here 是对算法思想的解释,以及它在 C 中的实现(我从未在 Android 上实现过,抱歉)。

    你也可以谷歌“point in polygon [你想要实现它的语言],互联网上有很多资源

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 2018-12-20
      • 2016-07-03
      相关资源
      最近更新 更多