2013-03-27 9 views
7

Ich habe eine Karte mit leaflet.js gezeichnet. Wenn ich den Längen- und Breitengrad als Eingabe gebe Kann ich das Polygon identifizieren? Kann ich ein clientseitiges Skript dafür bekommen?identifizieren, das ist das Polygon von Breite und Länge

+0

Sie lat und lng Wert für Polygon geben müssen shape.if Sie ein lat lng geben Wert es wird den Punkt repertn.while geben mehr lat lng es zeichnen polygon.start und ending lat lng sollte gleich sein. – tamilmani

+0

Ja, schauen Sie sich einen geodätischen Point-in-Polygon (PIP) -Algorithmus an. – pp19dd

+1

Ich habe eine Karte mit einer Reihe von Lat und Lng Werte gezeichnet. Aber jetzt möchte ich die Polygon-ID aus einem Satz von lat und lng-Wert wissen. – Sudha

Antwort

5

Haben Sie eine Antwort wie folgt:

// Das ist basierend auf 'Punkt in Polygon-Algorithmus'

function getPoint() { 
    float x=-89.82421875;  //x and y represents the lat and lng values 
    float y= 40.18307014852533; 
    var a = boundaries; //the coordinates used to draw the map 
    for (i = 0; i < a.features.length; i++) { 
     PointInPolygon(x,y, a.features[i].geometry.coordinates[0], i); 
    } 
}; 

function PointInPolygon(pointX, pointY, _vertices, number) { 
     var j = _vertices.length - 1; 
     var oddNodes = false; 
     var polyX, polyY,polyXJ,polyYJ; 

     for (var i = 0; i < _vertices.length; i++) { 
      polyY = parseFloat(_vertices[i].toString().split(",")[1]); 
      polyX = parseFloat(_vertices[i].toString().split(",")[0]); 
      polyXJ = parseFloat(_vertices[j].toString().split(",")[0]); 
      polyYJ = parseFloat(_vertices[j].toString().split(",")[1]); 
      if (polyY < pointY && polyYJ >= pointY || 
       polyYJ < pointY && polyY >= pointY) { 
       if (polyX + 
        (pointY - polyY)/(polyYJ - polyY) * (polyXJ - polyX) < pointX) 
       { 
        oddNodes = !oddNodes; 
       } 
      } 
      j = i; 
     } 
     if (oddNodes == true) { 
      map._layers[number+1].fire('click');    //fire the map click event 
     } 
    }