0

データベースからの情報を使用して、テキストフィールドにdblclickイベントを入力しようとしています。マーカーをダブルクリックすると、フィールドに入力するために誤った情報が使用されます。ダブルクリックしたマーカーでも同じ値が得られます。コリッド、ドライバー、車両の値を参照するためにさまざまな方法を試しましたが、何かを変更すると、フィールド内の不明な値しか得られません。

map = new google.maps.Map(document.getElementById("map"), map_options);
var infoWindow = new google.maps.InfoWindow;

downloadUrl(url, function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var ThisMarker = markers[i];
var InfoText = ThisMarker.getAttribute("InfoText");
var colid = ThisMarker.getAttribute("colid");
var driver = ThisMarker.getAttribute("driver");
var vehicle_reg = ThisMarker.getAttribute("vehicle");
var mtype = ThisMarker.getAttribute("type");
var point = new google.maps.LatLng(
    parseFloat(ThisMarker.getAttribute("lat")),
    parseFloat(ThisMarker.getAttribute("lng"))
);
var icon = customIcons[mtype] || {};
var marker = new google.maps.Marker({
    map: map,
    position: point,
    icon: icon.icon,
    clickable: true
});

bindInfoWindow(marker, map, infoWindow, InfoText);

if(mtype == 'col' || mtype == 'del') {
google.maps.event.addListener(marker, 'dblclick', function() {
document.getElementById("collivery_id").value = colid;
document.getElementById("command").value = 'action';
});
}

if(mtype == 'bike' || mtype == 'car' || mtype == 'bakkie' || mtype == 'truck') {
    google.maps.event.addListener(marker, 'dblclick', function() {
    document.getElementById("driver").value = driver;
    document.getElementById("vehicle").value = vehicle_reg;
});
}
}
});

function bindInfoWindow(marker, map, infoWindow, InfoText) {
    google.maps.event.addListener(marker, 'mouseover', function() {
    infoWindow.setContent(InfoText);
    infoWindow.open(map, marker);
});
google.maps.event.addListener(marker, 'mouseout', function() {
    infoWindow.close(map, marker);
});
}
4

1 に答える 1

0

関数クロージャを使用した可能な解決策:

map = new google.maps.Map(document.getElementById("map"), map_options);
var infoWindow = new google.maps.InfoWindow;

downloadUrl(url, function(data) {
  var xml = data.responseXML;
  var markers = xml.documentElement.getElementsByTagName("marker");
  for (var i = 0; i < markers.length; i++) {
    var ThisMarker = markers[i];
    var InfoText = ThisMarker.getAttribute("InfoText");
    var colid = ThisMarker.getAttribute("colid");
    var driver = ThisMarker.getAttribute("driver");
    var vehicle_reg = ThisMarker.getAttribute("vehicle");
    var mtype = ThisMarker.getAttribute("type");
    var point = new google.maps.LatLng(
      parseFloat(ThisMarker.getAttribute("lat")),
      parseFloat(ThisMarker.getAttribute("lng"))
    );
    var icon = customIcons[mtype] || {};
    var marker = new google.maps.Marker({
      map: map,
      position: point,
      icon: icon.icon,
      clickable: true
    });

    bindInfoWindow(marker, map, infoWindow, ThisMarker);
});

function bindInfoWindow(marker, map, infoWindow, marker)
{
  var InfoText = marker.getAttribute("InfoText");
  var colid = marker.getAttribute("colid");
  var driver = marker.getAttribute("driver");
  var vehicle_reg = marker.getAttribute("vehicle");
  var mtype = marker.getAttribute("type");
  if(mtype == 'col' || mtype == 'del') {
    google.maps.event.addListener(marker, 'dblclick', function() {
      document.getElementById("collivery_id").value = colid;
      document.getElementById("command").value = 'action';
    });
  }
  if(mtype == 'bike' || mtype == 'car' || mtype == 'bakkie' || mtype == 'truck') {
    google.maps.event.addListener(marker, 'dblclick', function() {
      document.getElementById("driver").value = driver;
      document.getElementById("vehicle").value = vehicle_reg;
    });
  }
  // whatever code is currently in your bindInfoWindow function
  // possibly this:
  google.maps.event.addListener(marker, 'mouseover', function() {
    infoWindow.setContent(InfoText);
    infoWindow.open(map, marker);
  });
  google.maps.event.addListener(marker, 'mouseout', function() {
    infoWindow.close(map, marker);
  });
}
于 2013-03-20T12:57:45.283 に答える