remember state after external link redirection

时间:2017-05-16 09:33:47

标签: javascript jquery html5 google-maps google-maps-api-3

I am having trouble trying to get the page to set a few localStorage variables; the problem is when I click on an internal link (like '/about') then the localStorage variables are set and the map retains the zoom and center positions but when I click on an external link (like google.com) on the website it does not update to the new position and zoom on the map when going back.

if(localStorage.lat) {
   map.setCenter(new google.maps.LatLng(localStorage.lat, localStorage.lng));
   map.setZoom(parseInt(localStorage.zoom)); 
}


$('a').on('click', function() { 
   localStorage.lat = map.getCenter().lat();
   localStorage.lng = map.getCenter().lng();
   localStorage.zoom = map.getZoom();
});

The external link is basically within an infoWindow which is open upon clicking the marker on google maps api v3.

var contentString = '<div class="infowindowcontent bk">'
contentString += '<p class="viewLink"><a href="external link"</a></p>' 
contentString += '</div>';

I am using infoWindow like this:

var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(contentString);

1 个答案:

答案 0 :(得分:1)

决定使用Google Maps Events来解决问题,而不是基于点击标记来解决问题,因为它没有在infoWindow(Google Map API函数)中拾取链接:

map.addListener('zoom_changed', function(){
  localStorage.zoom = map.getZoom();
});

map.addListener('center_changed', function(){
  localStorage.lat = map.getCenter().lat();
  localStorage.lng = map.getCenter().lng();
});