在地图上绘制标记实线和虚线的路径

时间:2015-07-30 07:25:13

标签: javascript google-maps-api-3

根据地图API v3的文档,有两个单独的虚线和实线示例..我有一个lat和long的数组,我想标记路径交替标记的实心和点缀之间的坐标..  代码是

    var lat_lng = new Array();
    var latlngbounds = new google.maps.LatLngBounds();
    for (var i = 0; i < markers.length; i++) {
        var data = markers[i];
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        lat_lng.push(myLatlng);
      var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title
        });
      //  console.log(i%2);

        var lineSymbol = {
              path: 'M 0,-1 0,1',
              strokeOpacity: 1,
              scale: 4
            };



        latlngbounds.extend(marker.position);
        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent("Location:"+data.location_name+"<br> Battery Status :"+data.battery_status+"<br> Form :"+data.tablename+"<br> Status :"+data.status+"<br> Time :"+new Date(data.time)+"<br> Weather :"+data.weather);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }
    map.setCenter(latlngbounds.getCenter());
    map.fitBounds(latlngbounds);
    //***********ROUTING****************//
    //Initialize the Path Array
    var path = new google.maps.MVCArray();
    //Initialize the Direction Service
    var service = new google.maps.DirectionsService();

    //Set the Path Stroke Color
    if(i%2==0)
  {
        var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7',strokeOpacity: 0,
              icons: [{
                icon: lineSymbol,
                offset: '0',
                repeat: '20px'
              }], });   
  }
else
  {
    var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7'});

  }

我最后用一个案例画线,但它一次只用一个实心或点缀。

1 个答案:

答案 0 :(得分:0)

单个google.maps.Polyline只能有一种样式(实体与虚线)。您需要将需要不同样式的每个段作为单独的折线。

proof of concept fiddle

代码段

&#13;
&#13;
var geocoder;
var map;
var markers = [{
  lat: 42,
  lng: -72,
  title: "0"
}, {
  lat: 43,
  lng: -73,
  title: "1"
}, {
  lat: 43,
  lng: -74,
  title: "2"
}, {
  lat: 42,
  lng: -75,
  title: "3"
}, {
  lat: 41,
  lng: -76,
  title: "4"
}];


function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var lat_lng = [];
  var latlngbounds = new google.maps.LatLngBounds();
  for (var i = 0; i < markers.length; i++) {
    var data = markers[i];
    var myLatlng = new google.maps.LatLng(data.lat, data.lng);
    lat_lng.push(myLatlng);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: data.title
    });
    //  console.log(i%2);

    var lineSymbol = {
      path: 'M 0,-1 0,1',
      strokeOpacity: 1,
      scale: 4
    };



    latlngbounds.extend(marker.position);
    (function(marker, data) {
      google.maps.event.addListener(marker, "click", function(e) {
        infoWindow.setContent("Location:" + data.location_name + "<br> Battery Status :" + data.battery_status + "<br> Form :" + data.tablename + "<br> Status :" + data.status + "<br> Time :" + new Date(data.time) + "<br> Weather :" + data.weather);
        infoWindow.open(map, marker);
      });
    })(marker, data);
    //***********ROUTING****************//
    if (lat_lng.length > 1) {
      //Set the Path Stroke Color
      if (i % 2 == 0) {
        var poly = new google.maps.Polyline({
          map: map,
          strokeColor: '#4986E7',
          strokeOpacity: 0,
          icons: [{
            icon: lineSymbol,
            offset: '0',
            repeat: '20px'
          }],
          path: lat_lng
        });
      } else {
        var poly = new google.maps.Polyline({
          map: map,
          strokeColor: '#4986E7',
          path: lat_lng
        });

      }
      lat_lng = [];
      lat_lng.push(myLatlng);


    }
  }
  map.setCenter(latlngbounds.getCenter());
  map.fitBounds(latlngbounds);
}
google.maps.event.addDomListener(window, "load", initialize);
&#13;
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
&#13;
&#13;
&#13;