简体   繁体   English

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

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

there has been some questions about latitude and longitude inside a polygon 关于多边形内部的经度和纬度存在一些问题

but i still don't get any answer for my problem 但我仍然无法解决我的问题

here is my problem : 这是我的问题:

let's say we have a polygon looks like a rectangle 假设我们有一个看起来像矩形的多边形

(in real problem , it won't shape only like a rectangle, it could have more than 2 points, maybe 100 points maximal) (在实际问题中,它的形状不会像矩形一样,可能会超过2个点,最大可能是100个点)

where 哪里

-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

(all latitude and longitude there is only an example) (所有经度和经度仅是一个示例)

Now that i have 4 point , there should be a polygon with 4 point , and try to draw a line from one point to another (there won't be any hole inside the polygon) 现在我有4个点,应该有一个具有4个点的多边形,并尝试从一个点到另一点画一条线(多边形内没有任何孔)

now let's say , i have 2 user 现在说,我有2个用户

Let's say 比方说

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

and

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

I need some algorithm or something that could determine if User A / B's coordinate inside the polygon 我需要一些算法或某些方法来确定用户A / B的坐标是否在多边形内

(something that will return true if user's coordinate is inside the polygon , or else will return false without displaying anything to the user) (如果用户的坐标在面内,则返回true,否则返回false而不向用户显示任何内容)

FYI 费耶

i have researched it using javascript 我已经使用javascript研究了它

here is my 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;
        }

as you can see 如你看到的

that code contains 2 functions (containsLatLng and getBounds) and with those functions, i could easily get the boolean if my latitude and longitude is inside a polygon 该代码包含2个函数(containsLatLng和getBounds),使用这些函数,如果我的经度和纬度在多边形内,则可以轻松获取布尔值

like this example : 像这个例子:

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

but that is javascript 但这是javascript

now in android, i don't know if there is such a thing like google.maps.Polygon or google.maps.LatLngBounds as you can see these two are required for my javascript function 现在在android系统中,我不知道是否有类似google.maps.Polygongoogle.maps.LatLngBounds之类的东西,因为您可以看到这两个是我的javascript函数所必需的

or is there any other ways to do something like this without depends on these two javascript functions? 还是有其他方法可以在不依赖这两个javascript函数的情况下进行类似的操作?

The problem you raised is commonly called Point in Polygon algorithm. 您提出的问题通常称为多边形点算法。
You should explain us where you'd like to implement it. 您应该向我们说明您想在哪里实现它。 In the view, server-side, or in your DB ? 在视图中,服务器端还是在数据库中?

However, here is the explaination of the algorithm's idea, and its implementation in C (I never implement it for Android, sorry). 但是, 是算法思想及其在C语言中的实现的解释(抱歉,我从来没有为Android实现它)。

You could also google "point in polygon [language in which you'd like to implement it], there are a lot of resources ont he Internet 您也可以在Google上输入“指向多边形[您想实现该语言的语言],Internet上有很多资源

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

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