I have a page that reloads it self every x seconds with
<meta http-equiv="refresh" content="60"/>
tag. There is a google map on the page and I need to remember the zoom level (and center) between refreshes.
This is what I have:
function initialize() {
// initialize the map
var latlng = new google.maps.LatLng(0, 0);
var myOptions = {
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
myMarkers = new Array();
updateMarkers();
cen = new google.maps.LatLng(-12.461334, 130.841904);
map.setCenter(cen);
zoomLevel = document.getElementById("zoomLevel").value;
map.setZoom(parseInt(zoomLevel));
google.maps.event.addListener(map, 'zoom_changed', function() {
str = map.getZoom() + '';
document.getElementById("zoomLevel").value = str;
});
}
So, I have a event listener that will update a hidden value zoomLevel after each zoom change. On each reload zoom level should be read from开发者_运维技巧 that value.
<input type="hidden" name="zoomLevel" id="zoomLevel" value="4" />
But after each refresh the zoom level is set to 4. What am I doing wrong?
Thanks.
The meta refresh isn't a postback, it's an actual clean request of the page. You have 2 ways to fix this:
- Stop refreshing in the first place, it's a horrible solution you came up with. Use ajax to request a new map and overwrite the old one in place.
- Worse, but maintains your weird style: use
setTimeout
instead and callform.submit
(or build the__doPostBack
link if you're using asp.net). This way you make an actual post request.
I believe that the solution is right at your fingertips. In fact you do not need to postback at all. Just schedule the initialize() function to be called every 60 seconds. So -
- Remove the auto-refresh META tag;
- Add a line something like: setInterval(initialize, 60000); in the page startup code
There you are.
Maybe you can use cookies to remember the state of your page.
精彩评论