2011-06-29 8 views
27

में बिंदु मौजूद है या नहीं, तो मैं Google मानचित्र v3 (जावास्क्रिप्ट) में बहुभुज के अंदर कोई बिंदु मौजूद है या नहीं, यह जांचने का एक तरीका ढूंढ रहा हूं। मैंने हर जगह खोज की है और पॉलीगॉन की सीमाओं को प्राप्त करने के लिए अब तक के एकमात्र समाधान किए गए हैं, लेकिन दिखाया गया कोड सिर्फ एक आयत बना रहा है और सभी प्रासंगिक बिंदुओं को शामिल करने के लिए अपने सतह क्षेत्र का विस्तार करता रहता है।Google मानचित्र v3: जांचें कि बहुभुज

वैसे, कारण मैं एक बड़े वर्ग का उपयोग नहीं कर सकता हूं यानी बहुभुज सीमाएं प्राप्त कर रहा हूं, यह है कि मेरे पास मानचित्र पर बहुभुज हैं और वे एक-दूसरे के क्षेत्र में विस्तार नहीं कर सकते हैं।

EDIT नीचे दिए गए उत्तर से निम्नलिखित के बाद, मैंने अपने मौजूदा बहुभुजों में से एक का उपयोग करके उदाहरण कोड को लागू करने का प्रयास किया है, लेकिन यह सिर्फ यह कह रहा है कि यह परिभाषित नहीं है और मैं क्यों नहीं समझ सकता।

यहाँ मेरी घोषणा है:

myCoordinates = [ 
    new google.maps.LatLng(0.457301,-0.597382), 
    new google.maps.LatLng(0.475153,-0.569916), 
    new google.maps.LatLng(0.494379,-0.563049), 
    new google.maps.LatLng(0.506738,-0.553436), 
    new google.maps.LatLng(0.520470,-0.541077), 
    new google.maps.LatLng(0.531456,-0.536957), 
    new google.maps.LatLng(0.556174,-0.552063), 
    new google.maps.LatLng(0.536949,-0.596008), 
    new google.maps.LatLng(0.503991,-0.612488), 
    new google.maps.LatLng(0.473780,-0.612488) ]; 

polyOptions = { 
    path: myCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 0.8, 
    strokeWeight: 2, 
    fillColor: "#0000FF", 
    fillOpacity: 0.6 }; 

var rightShoulderFront = new google.maps.Polygon(polyOptions); 
rightShoulderFront.setMap(map); 

और यहाँ है, जहां मैं बात के लिए जाँच कर रहा हूँ:

var coordinate = selectedmarker.getPosition(); 
var isWithinPolygon = rightShoulderFront.containsLatLng(coordinate); 
console.log(isWithinPolygon); 

लेकिन यह त्रुटि के साथ आ रहती है: Uncaught ReferenceError: rightShoulderFront परिभाषित नहीं है

उत्तर

36

इसे हल करने के लिए एक एल्गोरिदम रे-कास्टिंग है। एक स्पष्टीकरण here देखें।

और आप Google मानचित्र जेएस एपीआई वी 3 here के लिए इसे लागू करने वाले कोड को पा सकते हैं।

एचटीएच।

3

उदाहरण और कार्यान्वयन इस बात पर ध्यान नहीं देता है कि बहुभुज 180 डिग्री सीमा पार कर सकता है।

कार्यान्वयन इसे बाउंडिंग बॉक्स चेक में खाते में (अंतर्निहित) लेता है, लेकिन बहुभुज जांच विफल हो जाती है।

19

आप इसे Google मानचित्र ज्यामिति पुस्तकालय के साथ आसानी से कर सकते हैं।

पहले Google मानचित्र ज्यामिति पुस्तकालय को जोड़ना सुनिश्चित करें।

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script> 

फिर, परिभाषित अपने बहुभुज

var rightShoulderFront = new google.maps.Polygon({ 
      paths: myCoordinates 
     }); 
rightShoulderFront .setMap(map); 

मैं एक घटना 'क्लिक' को संभालने के लिए एक घटना श्रोता जोड़ने के लिए जा रहा हूँ, लेकिन आप अपनी आवश्यकताओं फिट करने के लिए अनुकूलित कर सकते हैं

google.maps.event.addListener(rightShoulderFront , 'click', isWithinPoly); 

हमारी क्लिक ईवेंट को संभालने के लिए एक फ़ंक्शन बनाएं, यदि Google की ज्यामिति लाइब्रेरी

का उपयोग करके बहुभुज के भीतर समन्वय मौजूद है या नहीं।
+2

कोई सीमा इस पद्धति के उपयोग पर गूगल द्वारा डाल दिया है, 'containsLocation()'? –

4

आपको Gmaps.js लाइब्रेरी के बारे में एक नज़र रखना चाहिए। भूगर्भ के बारे में यह एक बहुत ही सरल विधि है।

7

आपके पास Google मानचित्र API दस्तावेज़ में containsLocation() विधि का बहुत अच्छा example है।

1
var coordinate = new google.maps.LatLng(0.457301,-0.597382);//replace with your lat and lng values 
var isWithinPolygon = google.maps.geometry.poly.containsLocation(coordinate, yourPolygon); 

अपनी googleapis स्क्रिप्ट में लाइब्रेरी को शामिल करना न भूलें।Read more...

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry"></script> 
0

मैं एक ही बात और काम ठीक है और अपने ऑफ़लाइन कोड मैं इस कोड PHP में आप इसे किसी भी प्रोग्रामिंग भाषा में लिख सकते हैं लिखा है इस्तेमाल किया है।

class pointLocation { 
    var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices? 

    function pointLocation() { 
    } 

    function pointInPolygon($point, $polygon, $pointOnVertex = true) { 
     $this->pointOnVertex = $pointOnVertex; 

     // Transform string coordinates into arrays with x and y values 
     $point = $this->pointStringToCoordinates($point); 
     $vertices = array(); 
     foreach ($polygon as $vertex) { 
      $vertices[] = $this->pointStringToCoordinates($vertex); 
     } 

     // Check if the point sits exactly on a vertex 
     if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) { 
      return "vertex"; 
     } 

     // Check if the point is inside the polygon or on the boundary 
     $intersections = 0; 
     $vertices_count = count($vertices); 

     for ($i=1; $i < $vertices_count; $i++) { 
      $vertex1 = $vertices[$i-1]; 
      $vertex2 = $vertices[$i]; 
      if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary 
       return "boundary"; 
      } 
      if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) { 
       $xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x'])/($vertex2['y'] - $vertex1['y']) + $vertex1['x']; 
       if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal) 
        return "boundary"; 
       } 
       if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) { 
        $intersections++; 
       } 
      } 
     } 
     // If the number of edges we passed through is odd, then it's in the polygon. 
     if ($intersections % 2 != 0) { 
      return "inside"; 
     } else { 
      return "outside"; 
     } 
    } 

    function pointOnVertex($point, $vertices) { 
     foreach($vertices as $vertex) { 
      if ($point == $vertex) { 
       return true; 
      } 
     } 

    } 

    function pointStringToCoordinates($pointString) { 
     $coordinates = explode(" ", $pointString); 
     return array("x" => $coordinates[0], "y" => $coordinates[1]); 
    } 

} 

$pointLocation = new pointLocation(); 
$points = array("22.732965336387213 75.8609390258789"); 
$polygon = array("22.73549852921309 75.85424423217773","22.72346544538196 75.85561752319336","22.72346544538196 75.87175369262695","22.732332030848273 75.87295532226562","22.740406456758326 75.8686637878418","22.74198962160603 75.85407257080078"); 
echo '<pre>'; 
print_r($polygon); 
// The last point's coordinates must be the same as the first one's, to "close the loop" 
foreach($points as $key => $point) { 
    echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>"; 
} 

?>

संबंधित मुद्दे