Show a moving marker on the map

后端 未结 3 1995
星月不相逢
星月不相逢 2020-12-28 11:24

I am trying to make a marker move(not disappear and appear again) on the map as a vehicle moves on the road.

I have two values of latLng and I want

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 12:17

    For the marker to move relatively smoothly, you need to

    • Update more than every 1/10 fraction of the polyline (at least every few pixels)
    • Call the update method more frequently
    • Don't delete and re-add the marker

    For example, something like:

    var counter = 0;
    interval = window.setInterval(function() { 
      counter++;
      // just pretend you were doing a real calculation of
      // new position along the complex path
      var pos = new google.maps.LatLng(35, -110 + counter / 100);
      marker.setPosition(pos);
      if (counter >= 1000) {
        window.clearInterval(interval);   
      }
    }, 10);
    

    I made a simple example at http://jsfiddle.net/bmSbU/2/ which shows a marker moving along a straight path. If this is what you want, most of your code above regarding where along the line you are can be reused (or check out http://broady.github.io/maps-examples/points-along-line/along-directions.html )

提交回复
热议问题