1

我遇到了我的 infowindows 位置的问题。我在 for 循环中生成标记,但是当我单击信息窗口时,位置始终设置为最后生成的标记。不过内容是对的。这绝对是一个范围界定问题,还有一个 Drupal Ajax 调用可以获取内容(不包含任何位置信息)。任何人都可以帮忙吗?这是for循环:

for ( markerItem in data ) {

    var icon = icons[data[markerItem][3]].mouseOutIcon;
    var image = icons[data[markerItem][3]].mouseOverImage;
    var latlng = new google.maps.LatLng( data[markerItem][0], data[markerItem][1] );
    var marker = new google.maps.Marker({
                            icon:icon,
                            clickable:true,
                            position: latlng,
                            map: map,
                            draggable:false,
                            title: data[markerItem][2]

    });


    marker.mapid = mapid;
    marker.mid = data[markerItem][4];


    google.maps.event.addListener(marker,'click',function(latlng) {



        $.get('/map-info/'+this.mapid+'/'+this.mid, null, function(response) {
            console.log('first' + marker.getPosition());
            var result = Drupal.parseJson(response);
            if ( result.status == 0 ) {

                if(infowindow != null) {
                    infowindow.close();
                }

                infowindow = new google.maps.InfoWindow({
                    content: result.html
                    //position: marker.getPosition()

                });


                infowindow.open(map,marker);

            }
        });

        google.maps.event.addListener(marker, 'mouseover', function(latlng) {
            this.setImage( this.mouseOverImage );

        });
        google.maps.event.addListener(marker, 'mouseout', function(latlng) {
            this.setImage( this.mouseOutImage );
        });


    });



    marker.mouseOverImage = image;
    marker.mouseOutImage = icon.image;
    gmap.overlays.push( marker );
    marker.setMap(map);

    if ( checkboxState[data[markerItem][3]] ) {
        marker.setMap(map);
    }
    else {
        marker.setMap(null);
    }
}
});
}
else {
console.log('inside else');

var lat3 = gmap.oldBounds.getSouthWest().lat();
var lng3 = gmap.oldBounds.getSouthWest().lng();
var lat4 = gmap.oldBounds.getNorthEast().lat();
var lng4 = gmap.oldBounds.getNorthEast().lng();
$.get('/map-markers/'+mapid+'/'+lat1+'/'+lng1+'/'+lat2+'/'+lng2+'/'+lat3+'/'+lng3+'/'+lat4+'/'+lng4, null, function(response) {
var result = Drupal.parseJson(response);
var data = result.markers;
for ( var i = 0; i < 4; i++ ) {
    icons.push( { mouseOutIcon: Drupal.gmap.getIcon('boilers', i), mouseOverImage: mouseOverImage(Drupal.gmap.getIcon('boilers', i).image)});
}
var gmap = Drupal.gmap.getMap(mapid);
for ( markerItem in data ) {
    var icon = icons[data[markerItem][3]].mouseOutIcon;
    var image = icons[data[markerItem][3]].mouseOverImage;
    var latlng = new GLatLng( data[markerItem][0], data[markerItem][1] );
    var marker = createMarker(latlng, {icon: icon, title:data[markerItem][2], clickable:true, draggable:false }, data[markerItem][4] );
    marker.mouseOverImage = image;
    marker.mouseOutImage = icon.image;
    gmap.overlays.push( marker );
    map.addOverlay( marker );
    if ( checkboxState[data[markerItem][3]] ) {
        //marker.show();
    }
    else {
        //marker.hide();
    }
}
4

1 回答 1

1

你必须使用闭包来解决这个问题。

工作示例中的以下内容

使用下面代码中的 JS 方法AddInfoWidnow将 InfoWindow 添加到标记。

function AddInfoWidnow(marker,message)
{
     var infowindow = new google.maps.InfoWindow({ content: message });

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

     infowindow.open(marker.get('map'), marker);

    }); 

}

在 for 循环中调用AddInfoWidnow方法

function ShowOnMap(ContainerId, mapItems) {

var DefaultLatLng= new google.maps.LatLng('12.967461', '79.941824');

var mapOptions = {
    center: DefaultLatLng,
    zoom: 10,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    marker: true
};

var mapCust = new google.maps.Map(document.getElementById(ContainerId), mapOptions);

var arrJsonObject = JSON.parse(mapItems);

for (var y = 1; y <= arrJsonObject.length; y++) 
{

    var myLatLng1 = new google.maps.LatLng(arrJsonObject[y - 1].Latitude, arrJsonObject[y - 1].Lonngitude);

    var marker = new google.maps.Marker({
    position: myLatLng1,
    map: mapCust,
    title: 'Marked at '+ arrJsonObject[y - 1].markedDate
});

    addInfoWindows(marker,y-1,arrJsonObject);


    marker.setMap(mapCust);

}
}

使用下面的片段我称之为。

  var mapItems='[
  {
    "Latitude": "22.575897",
    "Lonngitude": "88.431754",
    "CustomerCode": "*$$$*",
    "CustomerName": "*@@@*",
    "markedDate": "2/20/2014 12:04:41 PM"
  },
  {
    "Latitude": "22.615067",
    "Lonngitude": "88.431759",
    "CustomerCode": "*$$$*",
    "CustomerName": "*@@@*",
    "markedDate": "2/20/2014 3:02:19 PM"
  }
]';
    ShowOnMap(containerId2, mapItems);

问题是您必须使用关闭添加信息窗口,这对我有用。

参考

于 2014-02-28T12:17:42.730 回答