-2

我一直在試圖顯示信息窗口,當我點擊谷歌地圖上的任何地方的問題。當你點擊地圖上的任何地方時,我想顯示一個標記和一個顯示經度和緯度的信息窗口?如何才能做到這一點?谷歌地圖在地圖上添加信息窗口點擊

這是我的代碼至今:你需要

<script type="text/javascript"> 

    var locations = <?php echo json_encode($js_array);?>; 
    var lats = <?php echo json_encode($lats);?>; 
    var lats = <?php echo json_encode($iphoto);?>; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 10, 
     center: new google.maps.LatLng(<?php echo $latitude;?>, <?php echo $longitude;?>), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

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

    var marker, i; 
    var latlngbound = new google.maps.LatLngBounds(); 

    for (i = 0; i < locations.length; i++) { 
     console.log(locations[i].lat); 
     // CREATE LATLNG OBJECT AND USE IT TO EXTEND THE LATLNGBOUNDS 
     var latlng = new google.maps.LatLng(locations[i].latitude, locations[i].longitude); 
     latlngbound .extend(latlng); 

     marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(locations[i].latitude, locations[i].longitude), 
      map: map, 
      animation: google.maps.Animation.DROP, 
      icon: {size: new google.maps.Size(30, 30), 
      scaledSize: new google.maps.Size(30, 30), 
      url: locations[i].iprofilephoto,} 
     }); 


     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
      return function() { 
       infowindow.setContent("<div><img style='width:40px;height:40px' src='"+locations[i].iprofilephoto+"'>"+locations[i].name+"</div><div><img style='width:100px;height:100px' src='"+locations[i].iphoto+"'></div>"); 
       infowindow.open(map, marker); 
      } 
     })(marker, i)); 
     map.fitBounds(latlngbound); 

    } 
</script> 
+0

否綁定地圖對象上的點擊事件偵聽器。 – MrUpsidown 2014-10-30 08:40:52

回答

0

一切都在documentation

google.maps.event.addListener(map, 'click', function(e) { 

    var marker = new google.maps.Marker({ 
     position: e.latLng, 
     map: map 
    }); 

    infowindow.setContent(e.latLng.toString()); 
    infowindow.open(map, marker); 
});