2012-03-13 22 views
7

से आयताकार निकालें मैं Google मानचित्र v3 (जावास्क्रिप्ट) का उपयोग कर रहा हूं।Google मानचित्र

<script type="text/javascript"> 
    // Global variables 
    var map; 

    /** 
    * Called on the initial page load. 
    */ 
    function init() { 

    map = new google.maps.Map(document.getElementById('map'), { 
     'zoom': 6, 
     'center': new google.maps.LatLng(41.87194,12.567379999999957), 
     'mapTypeId': google.maps.MapTypeId.ROADMAP 
    }); 

    //Region Overlay 
    var latLng1; 
    var latLng2; 

    <?php foreach ($this->arrRegion as $region) { ?> 
     latLng1 = new google.maps.LatLng(<?php echo $region['boundLat1_region']; ?>,<?php echo $region['boundLng1_region']; ?>); 
     latLng2 = new google.maps.LatLng(<?php echo $region['boundLat2_region']; ?>,<?php echo $region['boundLng2_region']; ?>); 
     redraw(latLng1,latLng2); 
    <?php }?> 

    } 

    /** 
    * Updates the Rectangle's bounds to resize its dimensions. 
    */ 
    function redraw(latLng1,latLng2) { 
    var latLngBounds = new google.maps.LatLngBounds(latLng1,latLng2); 
    // Create a new Rectangle overlay 
    var rectangle = new google.maps.Rectangle({map: map, bounds: latLngBounds}); 
    } 

    // Register an event listener to fire when the page finishes loading. 
    google.maps.event.addDomListener(window, 'load', init); 
</script> 

अब, मेरा लक्ष्य आयत दूर करने के लिए है: जब मैं अपने मानचित्र लोड मैं निम्नलिखित तरीके से एक आयत बनाएं। मैं नक्शा इस्तेमाल करने की कोशिश करता हूं। स्पष्ट लेकिन यह काम नहीं किया। कोई उपाय?

उत्तर

8

google.maps.Rectangle वर्ग एक setMap विधि है। यदि आप उस पर शून्य पास करते हैं, तो आयताकार हटा दिया जाता है। http://code.google.com/apis/maps/documentation/javascript/reference.html#Rectangle

ध्यान दें कि इसका मतलब है कि आपको अपने आयत के उदाहरणों को रखने की आवश्यकता है ताकि आप सेटमैप विधि को कॉल कर सकें। आपके रेड्रा फ़ंक्शन में स्थानीय आयताकार वैरिएबल इसे तब तक नहीं रखेगा जब तक कि आप फिर से उसी लैट्लिंग जोड़े को रेड्रो बुद्धि को कॉल न करें।

0

आप अशक्त इनपुट पैरामीटर के साथ setMap() फ़ंक्शन का उपयोग पर हर पुनः बनाने() कॉल कर सकते हैं।

// Global variables 
 
    var map; 
 
    var rectangle; 
 

 
    /** 
 
    * Called on the initial page load. 
 
    */ 
 
    function init() { 
 

 
    map = new google.maps.Map(document.getElementById('map'), { 
 
     'zoom': 6, 
 
     'center': new google.maps.LatLng(41.87194,12.567379999999957), 
 
     'mapTypeId': google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    //Region Overlay 
 
    var latLng1; 
 
    var latLng2; 
 

 
    <?php foreach ($this->arrRegion as $region) { ?> 
 
     latLng1 = new google.maps.LatLng(<?php echo $region['boundLat1_region']; ?>,<?php echo $region['boundLng1_region']; ?>); 
 
     latLng2 = new google.maps.LatLng(<?php echo $region['boundLat2_region']; ?>,<?php echo $region['boundLng2_region']; ?>); 
 
     redraw(latLng1,latLng2); 
 
    <?php }?> 
 

 
    } 
 

 
    /** 
 
    * Updates the Rectangle's bounds to resize its dimensions. 
 
    */ 
 
    function redraw(latLng1,latLng2) { 
 
    var latLngBounds = new google.maps.LatLngBounds(latLng1,latLng2); 
 
    
 
    // Remove Previous Rectangle 
 
    if(rectangle) 
 
     rectangle.setMap(null); 
 
    
 
    // Create a new Rectangle overlay 
 
    rectangle = new google.maps.Rectangle({map: map, bounds: latLngBounds}); 
 
    } 
 

 
    // Register an event listener to fire when the page finishes loading. 
 
    google.maps.event.addDomListener(window, 'load', init);