2014-06-16 34 views
0

我正在使用具有幾百個標記的Google Maps API JS v.3。我想打開一個具有特定於他們點擊的標記的信息的模式。我計劃使用PHP和AJAX來從MySQL數據庫的模態數據,但我不知道如何確定哪個標記點擊的用戶...帶有多個標記/模式的Google Maps API

任何幫助是極大的讚賞

`

 var map; 

     //icon variables 
     /* 
     var dem = 'path/to/blue/icon.png'; 
     var rep = 'path/to/red/icon.png'; 
     var oth = 'path/to/grey/icon.png'; 
     */ 
     var locations = [ 

      <?php 
       $c = 0; 
       $nr = mysqli_num_rows($gethouses); 
       while($z = mysqli_fetch_array($gethouses)): 
      ?> 
      ['<?php echo $z['name_first'].' '.$z['name_last']; ?>', <?php echo $z['lat']; ?>, <?php echo $z['lng']; ?>, 4], 
      <?php 

       $c++; 
       endwhile; 
      ?> 
     ]; 

     var map = new google.maps.Map(document.getElementById('google_world_map'), { 
      zoom: 10, 
      center: new google.maps.LatLng(39.675, -119.98), 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     var infowindow = new google.maps.InfoWindow(); 

     var marker, i; 
     var markers = new Array(); 

     for (i = 0; i < locations.length; i++) { 
      marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
      map: map 
     }); 

     markers.push(marker); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
      return function() { 
       infowindow.setContent(locations[i][0]); 
       infowindow.open(map, marker); 
      } 
      })(marker, i)); 
     } 

     function AutoCenter() { 
     // Create a new viewpoint bound 
     var bounds = new google.maps.LatLngBounds(); 
     // Go through each... 
     $.each(markers, function (index, marker) { 
     bounds.extend(marker.position); 
     }); 
     // Fit these bounds to the map 
     map.fitBounds(bounds); 
     } 
     AutoCenter(); 

     </script>` 
+0

你現有的代碼是什麼樣的? – geocodezip

+0

我將它添加到原始問題中。 :) – user3746522

回答

0

您可以在對象(理想值應該是唯一的)添加自定義參數。

for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map, 
     custom_param: some_unique_value 
    }); 

    .... 
} 

然後在您的標記click事件可以識別由在這種情況下marker.custom_param所獲得的價值點擊該標記。

google.maps.event.addListener(marker, 'click', (function(marker, i) { 
    return function() { 
     infowindow.setContent(locations[i][0]); 
     infowindow.open(map, marker); 

     // make use of `marker.custom_param` 
     var myCustomParam = marker.custom_param; 
    } 
    })(marker, i)); 
} 
+0

我得到它的工作。萬分感謝 :) – user3746522

相關問題