我使用'rightclick'动作在地图上添加标记。 每个标记都有其特定的infowindow,每个infowindow都有一个形式。
当我创建多个标记时,我的代码只触发第一个infowindow值,而不是好的值。
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.";
}
});
}
实际上,我想动态触发与点击的infowindow按钮相关的值。 我查看了“domready”动作,但无法理解。
答案 0 :(得分:0)
制作
var marker=[];
var infowindow=[];
在全球范围内。并尝试
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]);
});
});
同时在创建html varibale时添加infowindow索引
html += '<input type="button" onclick="saveData('+markIndex+')" class="info_button" value="Ajouter l\'emplacement"/>';
现在我们可以在你的saveData函数中关闭那个特定的infowindow
function saveData(index) {
........
if (responseCode == 200 && data.length <= 1) {
infowindow[index].close();
document.getElementById("message").innerHTML = "Location added.";
}
}
您可以看到用于创建多个infowindows here
的工作演示