2011-12-30 43 views
1

我使用谷歌地圖API V3與js和我試圖打開infowndow在地圖上,但通過我的代碼是不是在這裏打開每個標記是我的代碼示例請覈對告訴我哪裏是錯誤信息窗口上的谷歌地圖不工作

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

var markers = new Array(); 

function initialize() { 
    var map_center = new google.maps.LatLng(31.2330555556,72.3330555556); 


var GPS = <%=GPS %> 
    var myOptions = { 
     zoom: 8, 
     scaleControl:true, 
     pancontrol: true, 
     streetViewControl: true, 
     center: map_center, 
     mapTypeControl: true, 
     mapTypeControlOptions: { 
     style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
    }, 
     mapTypeId: google.maps.MapTypeId.HYBRID 
    }  
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    var infowindow = new google.maps.InfoWindow(); 


    for(i=0; i<GPS.length; i++) 
    { 

      var image = 'ico/no.png'; 

      var ContentString = GPS[i].TITLE; 
      markers[i] = new google.maps.Marker(
      { 
      position: GPS[i].GPS, 
      map: map, 
      draggable:true, 
      icon:image, 
      title:GPS[i].TITLE 
      });      

      google.maps.event.addListener(markers[i], 'click', function() { 
      infowindow.setContent(ContentString); 
      infowindow.open(map,markers[i]); 
      });    
     }  
     } 
</script> 
+0

任何人都請回復我的問題 – 2011-12-30 05:56:42

回答

0

試試下面的代碼:

google.maps.event.addListener(markers[i], 'click', function() { 
      infowindow.setContent(ContentString); 
      infowindow.open(map,this); 
      }); 
+0

感謝我回答 – 2011-12-30 07:41:14

0

我已經完成一個程序使用最近相同的API爲你的。面對同樣的問題,我發現關鍵因素是環ended.It據說變量「我」已經達到了最大化價值時的addListener的功能後工作的addListener 功能會工作。所以我加入了幾步來處理這個問題。你可以看看我的,我希望這是對你有幫助。

function ShowParkingPoints() { 

    var adNum=document.getElementById("tableOne").rows.length; 
    var i; 
    var j; 
    var l; 
    var ly; 
    for (i=1;i<adNum;i++) 
    { 
     for(j=1;j<5;j++) 
     { 
      var k=i-1; 

       addArray[k]+=document.getElementById("tableOne").rows[i].cells[j].innerHTML; 
     } 
    } 
    for (i=0;i<adNum-1;i++){ 
      var image = new sogou.maps.MarkerImage('images/flag.png', 
       new sogou.maps.Size(60, 60), 
       new sogou.maps.Point(0,0), 
       new sogou.maps.Point(0, 60)); 
      var shape = { 
       coord: [1, 1, 1, 20, 18, 20, 18 , 1], 
       type: 'poly' 
      }; 

      geocoder.geocode({ 'address': addArray[i]}, function(results, status) { 
       if (status == sogou.maps.GeocoderStatus.OK) { 
       var str=results[0].formatted_address; 
       //infowindow.setContent(str); 
       var marker1= new sogou.maps.Marker({ 
        map: map, 
       icon: image, 
        shape: shape, 
        draggable:true, 
        position: results[0].geometry.location 
       }); 
       i=i-1 
       markerArrayS[i]=marker1; 
       locationArray[i]=results[0].geometry.location; 
        sogou.maps.event.addListener(markerArrayS[i], 'click', function(event) { 
        makerClicked(event.latLng); 
      }); 
       } else { 
       alert("Geocode was not successful for the following reason: " + status); 
       } 
      }); 
    } 
    map.setCenter(mapCenter); 
      map.setZoom(13); 

} 
+0

是它爲我感謝 – 2011-12-30 07:50:04

+0

u能告訴我一些想法,使標記的集羣在我上面的代碼中好點子 – 2011-12-30 07:53:25

0

我想你只需要使用'i = i-1'來替換'我',當你開始登錄事件watcher.You可以試試看。在某種程度上,你需要的時候你要讀出來注意到在陣列,你的順序存儲。