I add markers on a map with the 'rightclick' action. Each marker has its specific infowindow and each infowindow has a form into it.
When I create more than one marker, my code only triggers the first infowindow values, not the good one.
google.maps.event.addListener(map, 'rightclick', function(event) {
var marker = new google.maps.Marker({
map: map,
draggable : true,
position: event.latLng,
animation: google.maps.Animation.DROP,
});
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': event.latLng}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
var html = "<div class='infobox'>";
html += "<p>";
html += "<strong>Lieu : </strong>";
html += results[0]['formatted_address'];
html += "</p>";
html += "<p>";
html += "<strong>Nombre de place (~) : </strong>";
html += "<input type='text' class='form_input' name='nb_place' id='nb_place' />";
html += "</p>";
html += "<p>";
html += "<strong>Point d'arrimage : </strong>";
html += "<input type='checkbox' id='point_arrim' name='point_arrim' />";
html += "</p>";
html += "<input type='hidden' id='formatted_address' name='formatted_address' value='"+results[0]['formatted_address']+"' />";
html += "<input type='hidden' id='geoloc_lat' name='geoloc_lat' value='"+event.latLng.nb+"' />";
html += "<input type='hidden' id='geoloc_long' name='geoloc_long' value='"+event.latLng.ob+"' />";
html += '<input type="button" onclick="saveData()" class="info_button" value="Ajouter l\'emplacement"/>'
html += "</div>";
var infowindow = new google.maps.InfoWindow({
map: map,
content : html
});
infowindow.open(map, marker);
google.maps.event.addListener(infowindow, 'domready', function(){
console.log("test");
console.log($(this));
});
}
});
});
function saveData() {
var nb_place = escape(document.getElementById("nb_place").value);
var point_arrim = escape(document.getElementById("point_arrim").checked);
var formatted_address = document.getElementById("formatted_address").value;
var geoloc_lat = document.getElementById("geoloc_lat").value;
var geoloc_long = document.getElementById("geoloc_long").value;
if(!nb_place) nb_place = null;
var url = "phpsqlinfo_addrow.php?nb_place=" + nb_place + "&point_arrim=" + point_arrim +
"&formatted_address=" + formatted_address + "&geoloc_lat=" + geoloc_lat + "&geoloc_long=" + geoloc_long;
downloadUrl(url, function(data, responseCode) {
if (responseCode == 200 && data.length <= 1) {
infowindow.close();
document.getElementById("message").innerHTML = "Location added.";
}
});
}
In fact, I want to dynamically trigger the values related to the clicked infowindow button. I looked at the 'domready' action but can't figure it out.
Make the
var marker=[];
var infowindow=[];
in a global scope. And try
google.maps.event.addListener(map, 'rightclick', function(event) {
marker.push(new google.maps.Marker({position: event.latLng, map: map}));
var markIndex = marker.length-1;
.......................
..........................
infowindow[markIndex] = new google.maps.InfoWindow({
content: html
});
infowindow[markIndex].open(map,marker[markIndex]);
//You can also add the info window again while clicking on marker if you need
google.maps.event.addListener(marker[markIndex], 'click', function(e) {
infowindow[markIndex].open(map,marker[markIndex]);
});
});
Also while creating the html varibale add the infowindow index to it
html += '<input type="button" onclick="saveData('+markIndex+')" class="info_button" value="Ajouter l\'emplacement"/>';
Now we can close that particular infowindow in your saveData function
function saveData(index) {
........
if (responseCode == 200 && data.length <= 1) {
infowindow[index].close();
document.getElementById("message").innerHTML = "Location added.";
}
}
You can see the working demo for creating multiple infowindows here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.