我正在尝试创建一个允许用户报告线索的地图 通过在infowindow中单击并填写表单来解决问题(类似于 seeclickfix.com)。我还没有内置实际的php / SQL 保存用户提供的信息。 问题:我的infowindow的html中有一个按钮供用户使用 提交信息并关闭窗口。而不只是关闭 虽然窗口,我的新标记的事件监听器会收到点击 通过infowindow,在按钮后面创建一个不需要的标记。 谷歌以某种方式避免了这个问题;他们没有注册点击 右上角'x'。 我试图创建一个布尔信号变量('infopen')来让 我的addMarker函数知道infowindow是否打开,但事实并非如此 功能....任何想法为什么?
有关代码的任何其他建议将不胜感激! (我的任务是创建一个组织和存储多个系统 信息窗口/标记...)
<?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>
答案 0 :(得分:0)
我的设计黑客:在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了。
我从这个代码示例中得到了这个想法: http://code.google.com/apis/maps/articles/phpsqlinfo_v3.html
在此示例中,infoWindow仅在发出数据库请求后关闭(在回调中)并确保响应良好。当然,这必须花费几毫秒。因此,根据这些假设,一旦你的数据库部分工作,你可以替换丑陋的setTimeout,一切都应该工作! :)