记住 Google 地图 v3 的状态

发布于 2024-10-17 22:06:33 字数 322 浏览 0 评论 0原文

我正在显示带有地标和 KML 叠加层的地图。当用户点击离开时,我希望在用户返回时记住地图的状态。

第一个问题是:我可以以某种方式保存地图的整个状态吗?覆盖层和一切?

目前,我一直在保存纬度/经度和缩放,然后重播任何 KML 叠加层,但还没有取得任何成功。我在边界更改或窗口卸载事件上保存cookie,但问题在于加载保存的值。原因是地图异步加载。我尝试过使用地图空闲事件,但它多次触发。似乎会触发,在处理程序中运行我的代码,然后在完成我的代码时再次触发。无限循环?无论如何,KML 叠加层的重播完全覆盖指定的任何纬度/经度/缩放。

所以主要问题是:如何恢复已保存的地图及其所有叠加层?

I am displaying a map with placemarks and KML overlays. When a user clicks away, I want the state of the map to be remembered for when the user returns.

First question is: can I save the entire state of the map somehow; overlays and everything?

For now, I have been saving the lat/long and zoom and then replaying any KML overlays, but haven't even had any success with this. I save cookies on the bounds changed or window unload events, but the problem is with loading the saved values. the reason being the map loads asynchronously. I've tried using the map idle event, but it fires more than once. Seems to fire, run my code in the handler, then fire again when it's finished my code. Infinite loop? Anyway, the replaying of the KML overlay completely overrides whatever lat/long/zoom was specified.

So main question is: how do you restore a saved map with all its overlays?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-10-24 22:06:33

我不认为谷歌地图可以保存地图的状态,你必须自己做。

您正确使用空闲事件来初始化覆盖。每当地图在缩放或平移后变得空闲时,都会触发空闲事件,因此如果您想要空闲事件(或任何其他事件)的一个时间侦听器,您可以这样做:

var map = new google.maps.Map(document.getElementById("map"), myOptions); 
var initListener = google.maps.event.addListener(map, 'idle', function() {
    // do your code here
    google.maps.event.removeListener(initListener);
});

如果您想保留地图的缩放和中心加载kml后,您必须使用KmlLayer的选项preserveViewport:

var kmlLayer = new google.maps.KmlLayer( myKml.kml, {preserveViewport: true});

I don't think Google Maps can save the state of the map, you have to do it by yourself.

You correctly used idle event to init the overlays. Idle event is triggered every time the map becomes idle after zooming or panning, so if you want one time listener for idle event (or any other event), you can do it this way:

var map = new google.maps.Map(document.getElementById("map"), myOptions); 
var initListener = google.maps.event.addListener(map, 'idle', function() {
    // do your code here
    google.maps.event.removeListener(initListener);
});

If you want to preserve zoom and center of the map after loading kml, you have to use KmlLayer's option preserveViewport:

var kmlLayer = new google.maps.KmlLayer( myKml.kml, {preserveViewport: true});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文