繁体   English   中英

Android GoogleMaps,如何检查多边形内的点(经度和纬度)

[英]Android GoogleMaps , How To Check Point (Latitude and Longitude) Inside a Polygon

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

但我仍然无法解决我的问题

这是我的问题:

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

(在实际问题中,它的形状不会像矩形一样,可能会超过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个函数(containsLatLng和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.Polygongoogle.maps.LatLngBounds之类的东西,因为您可以看到这两个是我的javascript函数所必需的

还是有其他方法可以在不依赖这两个javascript函数的情况下进行类似的操作?

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

但是, 是算法思想及其在C语言中的实现的解释(抱歉,我从来没有为Android实现它)。

您也可以在Google上输入“指向多边形[您想实现该语言的语言],Internet上有很多资源

暂无
暂无

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

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