I'm trying to use "setInterval" to reload a page that contains an embedded googlemaps.
When I use:
var marker = createMarker (point, id);
the page runs ok (one time). but when I try:
var marker = setInterval (function () {createMarker (point, id);}, 5000);
to automatically refresh the page, the following error occurs:
main.js:686Uncaught TypeError: Object 8 has no method 'initialize
Any ideas? thanks in advance!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<!--<META HTTP-EQUIV="refresh" CONTENT="15">-->
<title>Position</title>
<script src="http://maps.google.com/maps?file=api&v=2&key="
type="text/javascript"></script>
<script type="text/javascript" >
//<![CDATA[
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(32, 32),
new google.maps.Point(0, 0),
new google.maps.Point(16, 32)
);
function load() {
if (GBrowserIsCompatible()) {
//create map and center it
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
GDownloadUrl("gen_xml.php", function(data) {
var xml = GXml.parse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var id = markers[i].getAttribute("id");
var point = new GLatLng(
parseFloat(markers[i].getAttribute("latitude")),
parseFloat(markers[i].getAttribute("longitude"))
);
//var marker = createMarker(point, id);
var marker = setInterval(function(){createMarker(point, id);},5000);
map.addOverlay(marker);
map.setCenter(point,17);开发者_如何学Python
}
});
}
}
function createMarker(point, id) {
var marker = new GMarker(point, icon);
var html = "<b>" + id + "</b> <br/>" + point;
GEvent.addListener(marker, 'click', function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
//]]>
</script>
</head>
<body onload="load()" onunload="GUnload()">
<center>
<div id="map" style="width: 800px; height: 600px"></div>
</center>
</body>
</html>
setInterval
returns an integer, not the value that createMarker()
returns. Use the value returned by createMarker
in the setInterval
callback.
setInterval(function ()
{
var marker = createMarker(point, id);
map.addOverlay(marker);
map.setCenter(point, 17);
}, 5000);
You may also be seeing the classic JS function-in-a-loop problem.
精彩评论