2013-06-03 12 views
19

gibt es einen Algorithmus, um Marker innerhalb oder außerhalb von Polygon, Rechteck und Kreis zu überprüfen. Ich habe versucht, eine Funktion mit dieser link zu schreiben. aber kein Erfolg.Leaflet :: Wie man den Punkt innerhalb/außerhalb des Polygons oder Rechtecks ​​überprüft

+0

Für das, was es wert ist, Leaflet-API bietet für Rechtecke Grenzen überprüfen Sie die 'contains' Funktion. http://leafletjs.com/reference.html#bounds Keine Verwendung für Polygone und Kreise. –

+3

https://github.com/mapbox/leaflet-pip Ist ein Prospekt-Add-On, das eine Funktion zur Verfügung stellt, die die Raytracing-Technik implementiert, um zu bestimmen, ob ein Punkt innerhalb eines Polygons liegt. –

+0

Flugblatt-Pip sollte die akzeptierte Antwort sein. gut funktionieren (nach dem Patchen der Quelldatei: entfernen Sie "l instanceof L.MultiPolygon", https://github.com/mapbox/leaflet-pip/issues/8) – AlainIb

Antwort

5

Wenn Sie PHP dann funktioniert diese Funktion

$c = false; 

$vertices_x = array(22.333,22.222,22,444); //latitude points of polygon 
$vertices_y = array(75.111,75.2222,76.233); //longitude points of polygon 
$points_polygon = count($vertices_x); 
$longitude = 23.345; //latitude of point to be checked 
$latitude = 75.123; //longitude of point to be checked 

if (is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude, $latitude)){ 
    echo "Is in polygon!"."<br>"; 
} 
else { 
    echo "Is not in polygon"; 
} 

function is_in_polygon($points_polygon, $vertices_x, $vertices_y, $longitude_x, $latitude_y) { 
    $i = $j = $c = 0; 

    for ($i = 0, $j = $points_polygon-1; $i < $points_polygon; $j = $i++) { 
     if (($vertices_y[$i] > $latitude_y != ($vertices_y[$j] > $latitude_y)) && ($longitude_x < ($vertices_x[$j] - $vertices_x[$i]) * ($latitude_y - $vertices_y[$i])/($vertices_y[$j] - $vertices_y[$i]) + $vertices_x[$i])) { 
      $c = !$c; 
     } 
    } 

    return $c; 
} 
+1

Vielen Dank @Komal –

+0

wird diese Funktion mit Rechteck als funktioniert Gut? – user2485649

17

gibt es eine Funktion in Faltblatt dies zu überprüfen verwenden.

Polygon.getBounds().contains(MarketLatLng); 
+21

Dadurch wird nur geprüft, ob der Punkt innerhalb des Begrenzungsrechtecks ​​des Polygons liegt - nicht, wenn er vom Polygon selbst enthalten ist. –

+4

Um es intelligenter zu machen, verwenden Sie ** Ray-Casting-Algorithmus ** ([hier erklärt] (http://www.ecse.rpi.edu/Homepage/wrf/Research/Short_Notes/pnpoly.html)). [Beispiel der Implementierung] (https://github.com/substack/point-in-polygon/blob/master/index.js) – szymonm

+0

Der Arbeits-JavaScript-Code ist verfügbar [hier] (https://github.com/ Unterstapel/Punkt-in-Polygon/Blob/Master/Index.js). – Aneesh

0
<!-- <!DOCTYPE html> 
<html> 
    <head> 
    <title>Simple Map</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     #map-canvas { 
     margin: 0; 
     padding: 0; 
     width: 700px; 
     height: 500px; 
     } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
    <script> 
var map; 
var marker; 
var latlong = [["21.001663","75.486069"], 
["20.108977","73.914672"], 
["21.1458","79.088155"], 
["19.153061","77.305847"], 
["20.0831","73.79095"], 
["18.52043","73.856744"], 
["16.774832","74.486265"], 
["16.691308","74.244866"], 
["19.876165","75.343314"], 
["19.997453","73.789802"], 
["20.532949","76.184303"], 
["21.013321","75.563972"], 
["18.9513","72.82831"], 
["18.515752","73.182162"], 
["19.075984","72.877656"], 
["19.218331","72.97809"], 
["19.844006","79.36266"], 
["20.745319","78.602195"], 
["21.267052","78.577973"], 
["18.52043","73.856744"], 
["19.96955","79.304654"], 
["19.450585","72.799155"], 
["18.52043","73.856744"], 
["20.745319","78.602195"], 
["18.9833","75.7667"], 
["21.013321","75.563972"], 
["21.1458","79.088155"], 
["19.153061","77.305847"]]; 

function initialize() { 
    var mapOptions = { 
    zoom: 4, 
    center: new google.maps.LatLng(21.7679, 78.8718), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
     mapOptions); 
     drawCircle(); 
} 
function drawCircle() 
{ 
var options = { 
     strokeColor: '#800000', 
     strokeOpacity: 1.0, 
     strokeWeight: 1, 
     fillColor: '#C64D45', 
     fillOpacity: 0.5, 
     map: map, 
     center: new google.maps.LatLng(21.7679, 78.8718), 
     radius: 100000 
    }; 

    circle = new google.maps.Circle(options); 
    var bounds= circle.getBounds(); 
    for (var i = 0; i < latlong.length; i++){ 
    var hello =new google.maps.LatLng(latlong[i][0], latlong[i][1]); 
    if(bounds.contains(hello)) 
    { 
     marker= new google.maps.Marker({ 
     position:hello, 
     }); 
     marker.setMap(map); 
      console.log("Hi iam in bound"); 

      } 
      else 
      { 
      console.log("Iam not in bound"); 
} 
} 

} 
google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    </body> 
</html> --> 
+2

danke .. aber Ihr Code funktioniert mit Google Apis. und ich möchte Punkt mit Flugblatt apis überprüfen –

Verwandte Themen