Javascript 谷歌地图可以设置为缓慢的恒定平移吗?像一场全球革命?
正如标题所说。我正在寻找一个解决方案,但我没有找到任何指导我找到正确的文档或文章 如果您有任何想法或可以给我指出一个可能的解决方案,我将不胜感激Javascript 谷歌地图可以设置为缓慢的恒定平移吗?像一场全球革命?,javascript,jquery,google-maps,maps,panning,Javascript,Jquery,Google Maps,Maps,Panning,正如标题所说。我正在寻找一个解决方案,但我没有找到任何指导我找到正确的文档或文章 如果您有任何想法或可以给我指出一个可能的解决方案,我将不胜感激 谢谢你自己可以这样做: // Create your map and leave it in the "map" variable. setInterval(function() { map.panBy(x, 0); }, n); var map = new google.maps.Map(document.getElementById('m
谢谢你自己可以这样做:
// Create your map and leave it in the "map" variable.
setInterval(function() {
map.panBy(x, 0);
}, n);
var map = new google.maps.Map(document.getElementById('my-map'), mapOptions)
function animate () {
map.panBy(2.5 / 2, 1.3 / 2)
window.requestAnimationFrame(function () {
animate()
})
}
// eslint-disable-next-line
google.maps.event.addListenerOnce(map, "tilesLoaded", function () {
window.requestAnimationFrame(function () {
animate()
})
})
window.requestAnimationFrame(function () {
animate()
})
- 用于设置要定期运行的函数
- 该函数将调用GoogleMap对象来稍微“旋转”它
setInterval
指定的时间间隔,以获得平滑的效果。你应该有这样的东西:
// Create your map and leave it in the "map" variable.
setInterval(function() {
map.panBy(x, 0);
}, n);
var map = new google.maps.Map(document.getElementById('my-map'), mapOptions)
function animate () {
map.panBy(2.5 / 2, 1.3 / 2)
window.requestAnimationFrame(function () {
animate()
})
}
// eslint-disable-next-line
google.maps.event.addListenerOnce(map, "tilesLoaded", function () {
window.requestAnimationFrame(function () {
animate()
})
})
window.requestAnimationFrame(function () {
animate()
})
选择合适的
x
和n
值取决于您。可以接受的答案,但有点过时。您应该使用window.requestAnimationFrame
,而不是window.setInterval
。这将为您提供更平滑的动画
可以找到一个简单的例子(不是我的),但其要点如下:
// Create your map and leave it in the "map" variable.
setInterval(function() {
map.panBy(x, 0);
}, n);
var map = new google.maps.Map(document.getElementById('my-map'), mapOptions)
function animate () {
map.panBy(2.5 / 2, 1.3 / 2)
window.requestAnimationFrame(function () {
animate()
})
}
// eslint-disable-next-line
google.maps.event.addListenerOnce(map, "tilesLoaded", function () {
window.requestAnimationFrame(function () {
animate()
})
})
window.requestAnimationFrame(function () {
animate()
})