如何在滚动位置平移谷歌地图

时间:2013-04-21 14:53:33

标签: javascript jquery html google-maps

我有一个网站,其中背景由谷歌地图组成。 当用户滚动时,地图必须“移动”/平移到特定位置。 我找到了一个脚本,将谷歌地图设置为我的背景,所以这不是问题。

现在我的问题是:如何根据滚动位置使地图“移动”/平移?

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

使用API​​的.panTo方法(https://developers.google.com/maps/documentation/javascript/reference#Map)并从滚动事件中调用

类似

$(document).on('scroll', function(){
    // get current scroll position
    var currentScroll = $('element that scrolls').scrollTop();

    // calculate a new map point based on the currentScroll value
    var latLng = new google.maps.LatLng(somelat, somelong);

    // pan to it
    map.panTo(latLng); // assuming map refers to the map object
});
相关问题