简体   繁体   中英

How to pan google maps upon scroll position

I have this idea for an website where the background is made up of google maps. As the user scrolls the map has to "move"/pan to a specific location. I've found a script that will set google maps as my background so this is not the issue.

Now my question is: how do I make the map "move"/pan depending on scroll position?

Any help is greatly appreciated :)

Use the .panTo method of the API ( https://developers.google.com/maps/documentation/javascript/reference#Map ) and call it from a scroll event

something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM