Google maps api 3 单击侦听器冲突w/infowindow
我正在尝试创建一个允许用户报告跟踪工作的地图 在信息窗口中单击并填写表单时出现问题,类似于 请参阅clickfix.com。我还没有在php/SQL中构建能够 保存用户提供的信息。 问题是:我的信息窗口的html中有一个按钮,用户可以通过它来 提交信息并关闭窗口。而不是仅仅关闭 窗口中,我的新标记事件侦听器接收单击 通过信息窗口,在按钮后面创建一个不需要的标记。 谷歌以某种方式避免了这个问题;未在其上注册任何单击 右上角“x”出口。 我试图创建一个布尔信号变量“infopen”,以便 我的addMarker函数知道信息窗口是否打开,但它没有打开 运转。。。。你知道为什么吗 如果您对本规范有任何其他建议,我们将不胜感激! 我的任务是创建一个系统来组织和存储多个 信息窗口/标记Google maps api 3 单击侦听器冲突w/infowindow,google-maps-api-3,infowindow,Google Maps Api 3,Infowindow,我正在尝试创建一个允许用户报告跟踪工作的地图 在信息窗口中单击并填写表单时出现问题,类似于 请参阅clickfix.com。我还没有在php/SQL中构建能够 保存用户提供的信息。 问题是:我的信息窗口的html中有一个按钮,用户可以通过它来 提交信息并关闭窗口。而不是仅仅关闭 窗口中,我的新标记事件侦听器接收单击 通过信息窗口,在按钮后面创建一个不需要的标记。 谷歌以某种方式避免了这个问题;未在其上注册任何单击 右上角“x”出口。 我试图创建一个布尔信号变量“infopen”,以便 我的add
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Reporter </title>
<script type="text/javascript" src="http://maps.google.com/maps/
api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var marker;
var markers = [];
var infowindow;
var infopen = false;
var edit = true;
var pos = new google.maps.LatLng(44.021, -71.831102);
function initialize() {
var mapOptions = {
zoom: 14,
center: pos,
mapTypeControl: true,
panControl: false,
zoomControl: true,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var table = "<table>" +
"<tr><td>Problem:</td> <td><input type='text'
id='prob'/> </td> </tr>" +
"<tr><td>Description:</td> <td><input type='text'
size='30' id='desc'/></td> </tr>" +
"<tr><td align=right ><input type='button'
value='Save & Close' onclick='saveData()' /></td>" +
"<td><input type='button' value='Cancel & Delete'
onclick='cancel()' /></td></tr>";
infowindow = new google.maps.InfoWindow({
content: table
});
google.maps.event.addListener(map, "click", function(event) {
addMarker(event.latLng);
});
} //end initialize
// Add a marker to the map and push to the array, open an infowindow listener.
function addMarker(location) {
if (editon.editT[0].checked) edit = true; //check 'edit' radio buttons
if (editon.editT[1].checked) edit = false;
alert('infopen is ' + infopen);
if (edit== true && infopen== false) {
//if edit toggle is selected and infowindow not open
marker = new google.maps.Marker({
position: location, //from event.latLng
map: map,
draggable: true,
});
markers.push(marker); //add to markers array
google.maps.event.addListener(marker, "click", function() {
//listener for infowindow
infowindow.open(map, marker);
infopen = true; // stop the creation of new markers *in theory...
//alert('infopen is ' + infopen);
});
}// end if
} //end addMarker()
// Sets the map on all markers in the array. (only used when clearing)
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Deletes all markers in the array by removing references to
them.
function deleteOverlays() {
setAllMap(null);
markers = [];
}
//would passes along info to php... not yet
function saveData() {
infowindow.close();
infopen = false; //reallow marker creation from click
}
//closes window and clears marker
function cancel() {
infowindow.close();
infopen = false; // reallow marker creation ***click still
heard by event listener***
marker.setMap(null); //just clears from map
}
</script>
</head>
<body onload="initialize()">
<br> </br>
<form name="editon"> Turn on editing:
<input type="radio" name="editT" value='true' checked/>Yes
<input type="radio" name="editT" value='false' />No
</form>
<div id="map_canvas" style="width:100%; height:70%"></div>
<p>If too cluttered:
<input onclick="deleteOverlays();" type=button value="Clear Map"/>
</body>
</html>
我的人为攻击:在infowindow.close周围添加一个setTimeout。有一个理由,解释如下
function saveData() {
setTimeout(function() { infowindow.close(); }, 100);
}
//closes window and clears marker
function cancel() {
setTimeout(function() { infowindow.close(); marker.setMap(null); }, 100);
}
100毫秒似乎是合理的。奇怪的是,在cancel中将标记设置为空映射也必须超时。我认为你不再需要infopen了
我从这个代码示例中得到了这个想法:
在本示例中,只有在发出数据库请求并确保响应良好后,才会在回调中关闭infoWindow。当然,这必须花费一些毫秒。因此,在这些假设下,一旦DB部件正常工作,就可以替换丑陋的setTimeout,一切都应该正常工作!: 我明白了,聪明。这就足够了。我希望您是对的,数据库请求将同样执行。谢谢