javascript google map setcenter 和带动画的 setzoom

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19726622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:37:23  来源:igfitidea点击:

javascript google map setcenter and setzoom with animation

javascriptgoogle-maps

提问by bdogru

I am using google map api in javascript, i need to change zoom and center by the value coming from restful service and i can do that with setCenter and setZoom functions.

我在javascript中使用谷歌地图api,我需要通过来自restful服务的值更改缩放和中心,我可以使用setCenter和setZoom函数来做到这一点。

But the problem is that, map is reloading and my boss want it to focus that location with an animation not by reloading map is there anyway for doing so?

但问题是,地图正在重新加载,我的老板希望它通过动画而不是通过重新加载地图来聚焦该位置,无论如何这样做吗?

回答by akaHyman

The method setCenterreloads the map. For disabling map reloading and enabling the animation, use the panTomethod: Map.panTo Developers Documentation

该方法setCenter重新加载地图。要禁用地图重新加载并启用动画,请使用以下panTo方法:Map.panTo 开发人员文档

回答by Reza Baradaran Gazorisangi

According to this link, this method:

根据此链接,此方法:

panTo(latLng:LatLng|LatLngLiteral)

panTo(latLng:LatLng|LatLngLiteral)

Changes the center of the map to the given LatLng. If the change is less than both the width and height of the map, the transition will be smoothly animated.

将地图的中心更改为给定的 LatLng。如果变化小于地图的宽度和高度,过渡将平滑动画。

So you can simply use:

所以你可以简单地使用:

map.panTo(location);

where:

在哪里:

var location = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
// your map options
                };  
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // map-canvas is id of your map holder