【问题标题】:remember state after external link redirection记住外部链接重定向后的状态
【发布时间】:2017-05-16 09:33:47
【问题描述】:

我在尝试让页面设置一些 localStorage 变量时遇到问题;问题是当我点击一个内部链接(比如'/about')然后设置localStorage变量并且地图保留缩放和中心位置但是当我点击网站上的外部链接(比如google.com)时返回时不会更新到新位置并在地图上缩放。

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();
});

外部链接基本上在一个 infoWindow 内,点击 google maps api v3 上的标记即可打开。

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

我正在像这样使用 infoWindow:

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

【问题讨论】:

  • 请提供一个minimal reproducible example 来证明该问题。
  • @geocodezip 够了吗?我的意思是我不确定我还能给你什么?
  • 证明问题(在问题本身中)的code snippet 是我正在寻找的(不是很多断开连接的代码)

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


【解决方案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();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2023-03-31
    • 2018-09-05
    • 2012-02-19
    相关资源
    最近更新 更多