Multiple polyline to a marker on Google maps api v3

做~自己de王妃 提交于 2019-11-29 08:42:32

Make an array to hold all your polylines:

 var lines = [];

push your existing line(s) on that array:

 lines.push(line);

Process through them updating the icons.

function animate(){
  var count = 0;
  offsetId = window.setInterval(function(){
    count = (count + 1) % 2000;
    for (var i=0; i<lines.length; i++) {
     var icons = lines[i].get('icons');
     icons[0].offset = (count / 2) + '%';
     lines[i].set('icons', icons);
    }
  }, 200);
}// end of animate function

example

code snippet:

var line;
var lines = [];
var myLatlng = new google.maps.LatLng(41.7833, 5.2167);
var marker;

function initialize() {
    var styles = [{
      "featureType": "administrative.country",
      "stylers": [{
        "visibility": "off"
      }]
    }, {
      "featureType": "administrative",
      "elementType": "geometry",
      "stylers": [{
        "visibility": "off"
      }]
    }, {
      "featureType": "landscape",
      "stylers": [{
        "visibility": "on"
      }, {
        "color": "#C0C0C0"
      }]
    }, {
      "featureType": "water",
      "stylers": [{
        "visibility": "on"
      }, {
        "color": "#FFFFFF"
      }]
    }, {
      "featureType": "landscape.man_made",
      "stylers": [{
        "visibility": "off"
      }, {
        "color": "#efffff"
      }]
    }, {
      "featureType": "poi",
      "elementType": "geometry",
      "stylers": [{
        "visibility": "off"
      }]
    }, {
      "featureType": "transit",
      "stylers": [{
        "visibility": "off"
      }]
    }];

    var symbolOne = {
      strokeColor: '#F00',
      fillColor: '#F00',
      fillOpacity: 1
    };

    var domain = [new google.maps.LatLng(11.2583, 75.1374)];
    var markers = [];

    var mapOptions = {
      zoom: 1,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      opacity: 0.2,
      disableDefaultUI: true,
      draggable: false,
      styles: styles
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    var lineCoordinates = [
      new google.maps.LatLng(53.215556, 56.949219),
      new google.maps.LatLng(75.797201, 125.003906),
      new google.maps.LatLng(37.7833, 144.9667),
      new google.maps.LatLng(-24.797201, 26.003906),
      new google.maps.LatLng(27.797201, -101.003906)
    ];

    var lineSymbol = {
      path: google.maps.SymbolPath.FORWARD_OPEN_ARROW
    };

    for (i = 0; i < lineCoordinates.length; i++) {
      markers.push(new google.maps.Marker({
        position: lineCoordinates[i],
        map: map
      }));

      line = new google.maps.Polyline({
        path: [lineCoordinates[i], domain[0]],
        strokeOpacity: 0.8,
        strokeWeight: 2,
        strokeColor: '#f00',
        geodesic: true,
        icons: [{
          icon: lineSymbol,
          offset: '100%',
          repeat: '30px'
        }]
      });
      line.setMap(map);
      lines.push(line);
    } //end of for loop
    // alert(lines.length);
    animate();

  } //end of initialize function

function animate() {
    var count = 0;
    offsetId = window.setInterval(function() {
      count = (count + 1) % 2000;
      for (var i = 0; i < lines.length; i++) {
        var icons = lines[i].get('icons');
        icons[0].offset = (count / 2) + '%';
        lines[i].set('icons', icons);
      }
    }, 200);
  } // end of animate function
google.maps.event.addDomListener(window, 'load', initialize);
html {
  height: 100%;
  width: 100%;
}
body {
  height: 100%;
  width: 100% margin: 0;
  padding: 0
}
#map_canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="width: 100%; height: 100%; "></div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!