Ich verwende Google Entfernung Matrix, um Entfernungen zwischen verschiedenen Zielen gleichzeitig zu berechnen. Ich habe einen Ursprung und mehrere Ziele, und ich möchte Infowindows mit diesen Daten (die Entfernung zum Ziel) hinzufügen können. Das Ergebnis, das ich im Moment bekomme, ist, dass die Adresse nicht definiert ist. Dies liegt daran, dass es sich außerhalb der for-Schleife befindet, aber ich bin nicht sicher, wie ich das beheben kann.Google Maps Infowindow mit dynamischen Daten
Mein Code:
function callback(response, status) {
if(status=="OK") {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var bounds = new google.maps.LatLngBounds;
var outputDiv = document.getElementById('output');
var showGeocodedAddressOnMap = function(asDestination) {
var icon = asDestination ? destinationIcon : originIcon;
return function(results, status) {
if (status === 'OK') {
map.fitBounds(bounds.extend(results[0].geometry.location));
var markersArray = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
google.maps.event.addListener(markersArray, 'click', function() {
infowindow.setContent('<strong>Address</strong><br /><br />' + destinationList[j] + '<br /><br /><strong>Distance:</strong> ');
infowindow.open(map, this);
});
} else {
alert('Geocode was not successful due to: ' + status);
}
};
};
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
geocoder.geocode({'address': originList[i]},
showGeocodedAddressOnMap(false));
for (var j = 0; j < results.length; j++) {
geocoder.geocode({'address': destinationList[j]}, showGeocodedAddressOnMap(true));
/*outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + '<br>';*/
}
}
} else {
alert("Error: " + status);
}
}