Pregunta

Estoy tratando de probar si una polilínea de Google Maps pasa por un polígono de Google Maps.Suena sencillo.Pero he buscado y buscado ... y no encontré respuestas reales.

Lo más cerca que pude fue esta función.Funciona pero, frustrantemente, devuelve el falso positivo ocasional.

//nvert = the number of points in the polygon
//vertx = an array of all the polygon's latitudes
//verty = an array of all the polygon's longitudes
//elat = the current point's latitude
//elng = the current point's longitude

function pnpoly( nvert, vertx, verty, elat, elng) {
        var i, j, c = false;
        for( i = 0, j = nvert-1; i < nvert; j = i++ ) {
            if( ( ( verty[i] > elng ) != ( verty[j] > elng ) ) &&
                ( elat < ( vertx[j] - vertx[i] ) * ( elng - verty[i] ) / ( verty[j] - verty[i] ) + vertx[i] ) ) {
                    c = !c;
            }
          }
          return c;
    }

Antes de probar un método completamente nuevo (una loca idea matemática que traevolver al cálculo de grado 12), me pregunto si alguien sabe cómo lograrlo.

¿Fue útil?

Solución

Me he encontrado con una solución que funciona.

https://github.com/albertsun/JavaScript-Geometry

Este paquete de geometría incluye una función llamada findIntersections().

Ejecuté un bucle $.each en cada polígono en mi mapa, luego empujé cada punto del polígono en una matriz, luego cada punto de la polilínea en una matriz.Finalmente, ejecuté dos bucles y empujé las coordenadas lat / lon en variables para la función.Vuelve vacío cuando no encuentra nada y devuelve las coordenadas de intersección cuando encuentra algo.

function processPath(polyline, polygons){
$.each(polygons, function(i,polygon){
    var polygonArr = [] // array for storing each point in polygon

    polygon.getPaths().forEach(function(k,g){
        $.each(k.b, function(l,m){
            polygonArr.push({'lat':m.lat(),'lng':m.lng()});
        });
    });

    //Get the number of points in the polyLINE
    var numStops = polyline.getPath().b.length -1;

    //Get the path and coordinates of the polyLINE
    var polylineArr = [];

    polyline.getPath().forEach(function(z,y){
        polylineArr.push({'lat':z.lat(),'lng':z.lng()});
    });
    $.each(polygonArr, function(j, polygon){
        $.each(polylineArr, function(k, polyline){
            if(k+1 != polylineArr.length){
                var lineCoor1x = polylineArr[k].lat;
                var lineCoor1y = polylineArr[k].lng;
                var lineCoor2x = polylineArr[k+1].lat;
                var lineCoor2y = polylineArr[k+1].lng;
                var polyCoorx = polygonArr[j].lat;
                var polyCoory = polygonArr[j].lng;
                if(j+1 == polygonArr.length){
                    // We've reached the end, go back to the start
                    var polyCoorNextx = polygonArr[0].lat
                    var polyCoorNexty = polygonArr[0].lng
                } else {
                    // Go to the next point
                    var polyCoorNextx = polygonArr[j+1].lat
                    var polyCoorNexty = polygonArr[j+1].lng
                }
                if(findIntersections([[[lineCoor1x,lineCoor1y], [lineCoor2x,lineCoor2y]], [[polyCoorx,polyCoory],[polyCoorNextx,polyCoorNexty]] ]).length != 0){
                    whereInside[i] = i;
                    return;
                }
            }
        })
    })

Probablemente sea un poco complicado, pero funciona.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top