简体   繁体   English

使用 Google Maps api v3 在折线上移动标记

[英]Moving Marker on a PolyLine using Google Maps api v3

I am using Google Maps API V3.我正在使用谷歌地图 API V3。 I am trying to animate a marker on the Polyline smoothly.我正在尝试平滑地为折线上的标记设置动画。

I have Tried this http://jsfiddle.net/bmSbU/154/我试过这个http://jsfiddle.net/bmSbU/154/

Here I have made fixed points as (30,-110) and (30,-100) so I can able to make based on the fixed points.在这里,我将固定点设为 (30,-110) 和 (30,-100),因此我可以根据固定点进行制作。

Now my question is how to do the same when I have multiple points (PolyLine) and the marker should move smoothly without any flicking on map.现在我的问题是当我有多个点(折线)时如何做同样的事情,并且标记应该平滑移动而不会在地图上轻弹。

var map;
var path;
var marker;

function initialize() {
    var myLatlng = new google.maps.LatLng(35, -105);
    var myOptions = {
        zoom: 5,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

    route = new google.maps.Polyline({
        path: [
        new google.maps.LatLng(30, -110),
        new google.maps.LatLng(30, -100)],
        map: map
    });

    marker = new google.maps.Marker({
        position: new google.maps.LatLng(30, -110),
        map: map
    });

    counter = 0;
    interval = window.setInterval(function () {
        counter++;
        var pos = new google.maps.LatLng(30, -110 + counter / 100);
        marker.setPosition(pos);
        if (counter >= 1000) {
            window.clearInterval(interval);
        }
    }, 10);
}

google.maps.event.addDomListener(window, 'load', initialize);

Can anybody help me out?有人可以帮帮我吗?

Your jsfiddle plays smoothly for me on the latest version of Safari on a newer macbook pro.您的 jsfiddle 在较新的 macbook pro 上的最新版本的 Safari 上对我来说很流畅。 YMMV with different hardware/platforms.具有不同硬件/平台的 YMMV。

Fundamentally, CSS animations generally outperform similar animations implemented in Javascript.从根本上说,CSS 动画通常优于用 Javascript 实现的类似动画。 I think the Google Maps API is going to cause animation artifacts when you call Marker#setPosition() via timeout internals.我认为当您通过超时内部调用 Marker#setPosition() 时,Google Maps API 会导致动画伪影。 See this answer for How to add custom animation on Google Map V3 Marker when I drop each marker one by one?当我一个一个地放下每个标记时,请参阅此答案以了解如何在 Google Map V3 标记上添加自定义动画? for a deep dive into hacking how Google internally implements the google.maps.Marker#setAnimation method using CSS animations.深入了解 Google 如何在内部使用 CSS 动画实现google.maps.Marker#setAnimation方法。

Another option is to stop using Google's Marker type, and implement a custom marker type that supports custom CSS animation.另一种选择是停止使用 Google 的 Marker 类型,并实现支持自定义 CSS 动画的自定义标记类型。 This is not as hard as it sounds.这并不像听起来那么难。 Check out a blog post by Mike Bostock on using D3 for custom markers on Google Maps .查看 Mike Bostock 关于在 Google 地图上使用 D3 进行自定义标记的博客文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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