减慢谷歌 panTo 功能

发布于 2025-01-06 12:48:04 字数 97 浏览 1 评论 0原文

我有一张地图,当标记被放置在地图上时,它可以在地图周围从一个点平移到另一个点。我遇到的问题是平移太快。有什么办法可以减慢 panTo 功能的速度吗?

谢谢, 克里斯

I have a map that pans from point to point around a map as markers are dropped on the map. The issue I'm having is that the panning is too fast. Is there any way to slow down the panTo function?

Thanks,
Chris

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

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

发布评论

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

评论(3

潦草背影 2025-01-13 12:48:04

遗憾的是,不,您无法更改 panTo 动画的速度。

该函数仅接受一个 latlng 参数。详细信息请参见:http://code.google.com/apis/地图/documentation/javascript/reference.html#Map

Sadly, no, you cannot change the speed of the panTo animation.

The function only takes a single latlng argument. Details here: http://code.google.com/apis/maps/documentation/javascript/reference.html#Map

℡寂寞咖啡 2025-01-13 12:48:04

我编写了自己的 panTo 实现。使用“EasingAnimator”类。

var EasingAnimator = function(opt){
        opt = opt || {};
        this.easingInterval = opt.easingInterval;
        this.duration = opt.duration || 1000;
        this.step = opt.step || 50;
        this.easingFn = opt.easingFn  || function easeInOutElastic(t, b, c, d) {
            if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
            return -c/2 * ((t-=2)*t*t*t - 2) + b;
        };
        this.callBack = opt.callBack || function(){};
    };
EasingAnimator.makeFromCallback = function(callBack){
    return new EasingAnimator({
        callBack: callBack
    });
};
EasingAnimator.prototype.easeProp = function(obj, propDict){
    propDict = propDict || {};

    var self = this,
        t = 0,
        out_vals = JSON.parse(JSON.stringify(obj));

    clearInterval(self.easingInterval);
    self.easingInterval = setInterval(function(){
        t+= self.step;
        if (t >= self.duration) {
            clearInterval(self.easingInterval);
            self.callBack(propDict);
            return;
        }
        var percent = self.easingFn(t, 0, 1, self.duration);
        Object.keys(propDict).forEach(function(key, i) {
            var old_val = obj[key];

            out_vals[key] = old_val - percent*(old_val - propDict[key]);
        });
        self.callBack(out_vals);
    }, self.step);
};

现在您可以控制一切,包括持续时间、步骤,当然还有缓动功能。这里有一些很好的例子http://easings.net/。现在您可以像这样使用它:

dom_elem.addEventListener('click', function(event){
    var point = map.getCenter();

    easingAnimator.easeProp({
        lat: point.lat(),
        lng: point.lng()
    }, points[i]);
});

在这里您可以找到其工作原理的现场演示
http://codepen.io/ErDmKo/pen/Jdpmzv

I write my own implementation of panTo. Using class "EasingAnimator".

var EasingAnimator = function(opt){
        opt = opt || {};
        this.easingInterval = opt.easingInterval;
        this.duration = opt.duration || 1000;
        this.step = opt.step || 50;
        this.easingFn = opt.easingFn  || function easeInOutElastic(t, b, c, d) {
            if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
            return -c/2 * ((t-=2)*t*t*t - 2) + b;
        };
        this.callBack = opt.callBack || function(){};
    };
EasingAnimator.makeFromCallback = function(callBack){
    return new EasingAnimator({
        callBack: callBack
    });
};
EasingAnimator.prototype.easeProp = function(obj, propDict){
    propDict = propDict || {};

    var self = this,
        t = 0,
        out_vals = JSON.parse(JSON.stringify(obj));

    clearInterval(self.easingInterval);
    self.easingInterval = setInterval(function(){
        t+= self.step;
        if (t >= self.duration) {
            clearInterval(self.easingInterval);
            self.callBack(propDict);
            return;
        }
        var percent = self.easingFn(t, 0, 1, self.duration);
        Object.keys(propDict).forEach(function(key, i) {
            var old_val = obj[key];

            out_vals[key] = old_val - percent*(old_val - propDict[key]);
        });
        self.callBack(out_vals);
    }, self.step);
};

Now you can control everything including duration, steps and of course the easing function. Here are some nice examples of it http://easings.net/. And now you can use it some like this:

dom_elem.addEventListener('click', function(event){
    var point = map.getCenter();

    easingAnimator.easeProp({
        lat: point.lat(),
        lng: point.lng()
    }, points[i]);
});

Here you can find live demo of how it works
http://codepen.io/ErDmKo/pen/Jdpmzv

雾里花 2025-01-13 12:48:04

我编写了一个函数来使用 Google Maps API v3 实现“慢摇”。它使用小平移步骤以及之前的答案,但我认为实现更简单一些。您可以对 f_timeout() 使用缓动函数。

参数

map:您的 google.maps.Map 对象

endPosition:所需的平移位置,google.maps.LatLng

n_intervals:平移间隔数,越多过渡越平滑,但性能越差

T_msec:慢摇镜头完成的总时间间隔(毫秒)

var slowPanTo = function(map, endPosition, n_intervals, T_msec) {
  var f_timeout, getStep, i, j, lat_array, lat_delta, lat_step, lng_array, lng_delta, lng_step, pan, ref, startPosition;
  getStep = function(delta) {
    return parseFloat(delta) / n_intervals;
  };
  startPosition = map.getCenter();
  lat_delta = endPosition.lat() - startPosition.lat();
  lng_delta = endPosition.lng() - startPosition.lng();
  lat_step = getStep(lat_delta);
  lng_step = getStep(lng_delta);
  lat_array = [];
  lng_array = [];
  for (i = j = 1, ref = n_intervals; j <= ref; i = j += +1) {
    lat_array.push(map.getCenter().lat() + i * lat_step);
    lng_array.push(map.getCenter().lng() + i * lng_step);
  }
  f_timeout = function(i, i_min, i_max) {
    return parseFloat(T_msec) / n_intervals;
  };
  pan = function(i) {
    if (i < lat_array.length) {
      return setTimeout(function() {
        map.panTo(new google.maps.LatLng({
          lat: lat_array[i],
          lng: lng_array[i]
        }));
        return pan(i + 1);
      }, f_timeout(i, 0, lat_array.length - 1));
    }
  };
  return pan(0);
};

I wrote a function to implement a "slow pan" with Google Maps API v3. It uses small pan steps as well as the previous answer, though I think the implementation is a bit simpler. You may use an easing function for f_timeout().

Parameters

map: your google.maps.Map object

endPosition: desired location to pan to, google.maps.LatLng

n_intervals: number of pan intervals, the more the smoother the transition but the less performant

T_msec: the total time interval for the slow pan to complete (milliseconds)

var slowPanTo = function(map, endPosition, n_intervals, T_msec) {
  var f_timeout, getStep, i, j, lat_array, lat_delta, lat_step, lng_array, lng_delta, lng_step, pan, ref, startPosition;
  getStep = function(delta) {
    return parseFloat(delta) / n_intervals;
  };
  startPosition = map.getCenter();
  lat_delta = endPosition.lat() - startPosition.lat();
  lng_delta = endPosition.lng() - startPosition.lng();
  lat_step = getStep(lat_delta);
  lng_step = getStep(lng_delta);
  lat_array = [];
  lng_array = [];
  for (i = j = 1, ref = n_intervals; j <= ref; i = j += +1) {
    lat_array.push(map.getCenter().lat() + i * lat_step);
    lng_array.push(map.getCenter().lng() + i * lng_step);
  }
  f_timeout = function(i, i_min, i_max) {
    return parseFloat(T_msec) / n_intervals;
  };
  pan = function(i) {
    if (i < lat_array.length) {
      return setTimeout(function() {
        map.panTo(new google.maps.LatLng({
          lat: lat_array[i],
          lng: lng_array[i]
        }));
        return pan(i + 1);
      }, f_timeout(i, 0, lat_array.length - 1));
    }
  };
  return pan(0);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文