如何使用JavaScript为Google地图中的所有标记设置infowindow?

时间:2014-11-19 12:26:46

标签: javascript google-maps-api-3

我为卡车停在的位置创建了停止点标记。但是当点击所有停止点时,我需要一些帮助来打开infowindow。

我的js代码:

   var ImageStop  = '/images/StopPoint.gif';
   var infoWindowTrcukStop = new google.maps.InfoWindow();
   var json = result.d;
   obj = JSON.parse(json);

     for (var i = 0; i < obj.length - 1; i++)

     {

       if(parseInt(obj[i].Speed)==0)
      {
       var LatLngStop = new google.maps.LatLng(obj[i].Lat, obj[i].Lng);
       markerStopTruck = new google.maps.Marker({
       position: LatLngStop,
       draggable: true,
       animation: google.maps.Animation.DROP,
       map: map,
       title: 'Stopped Here',
       icon: ImageStop,

      });

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

        infoWindowTruckStop.setContent(this.title);
        infoWindowTruckStop.open(map, markerStopTruck);


        });

       markerStopTruck.setMap(map);
      }
      }

1 个答案:

答案 0 :(得分:0)

我解决了。

            for (var i = 0; i < obj.length - 1; i++)  {
               if(parseInt(obj[i].Hiz)==0)  {
                var LatLngStop = new google.maps.LatLng(obj[i].Lat, obj[i].Lng);
                              markerStopTruck= new google.maps.Marker({
                                  position: LatLngStop,
                                  draggable: true,
                                  animation: google.maps.Animation.DROP,
                                  map: map,
                                  title: '',
                                  icon: ImageStop,
                              });
                              var content = 'Stop Here';
                              google.maps.event.addListener(markerStopTruck, 'click', (function 
                               (markerStopTruck, content, infoWindowTruckStop) {
                                  return function () {
                                      infoWindowTruckStop.setContent(content);
                                      infoWindowTruckStop.open(map, markerStopTruck);
                                  };
                              })(markerStopTruck, content, infoWindowTruckStop));


                              markerStopTruck.setMap(map);

                          }  }