一起实施Google Maps Polyline和标记

时间:2019-01-28 22:45:33

标签: google-maps-markers

您好,我有一个代码,可以在地图上查看经纬度标记,我遵循此tutorial所做的代码,我想知道如何使每个标记与多段线链接

这就是我所拥有的

enter image description here

这就是我想要的

enter image description here

接下来我留下我的代码

    function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(-16.342024,-71.540503),
      zoom: 4.0
    });
    var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP or XML file
      downloadUrl('../controlador/test1.xml', function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName('marcadores');
        Array.prototype.forEach.call(markers, function(markerElem) {
          var name = markerElem.getAttribute('name');
          var address = markerElem.getAttribute('address');
          var type = markerElem.getAttribute('movil');
          var hora = markerElem.getAttribute('hora');
          var bateria = markerElem.getAttribute('bateria');
          var point = new google.maps.LatLng(
              parseFloat(markerElem.getAttribute('lat')),
              parseFloat(markerElem.getAttribute('lng')));

          var infowincontent = document.createElement('div');
          var strong = document.createElement('strong');
          strong.textContent = name
          infowincontent.appendChild(strong);
          infowincontent.appendChild(document.createElement('br'));



          var text = document.createElement('text');
           text.textContent = address
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label,
            animation: google.maps.Animation.DROP
          });

     var text = document.createElement('br');
          text.textContent = address
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });
      var text = document.createElement('text');
          text.textContent = "MOVIL: "+type
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });
       var text = document.createElement('br');
          text.textContent = address
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });

       var text = document.createElement('text');
          text.textContent = "HORA: "+hora
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });

       var text = document.createElement('br');
          text.textContent = address
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });

       var text = document.createElement('text');
          text.textContent = "BATERIA: "+bateria+"%"
          infowincontent.appendChild(text);
          var icon = customLabel[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            label: icon.label
          });  


          marker.addListener('click', function() {
            infoWindow.setContent(infowincontent);
            infoWindow.open(map, marker);
          });
        });
      });
    }



  function downloadUrl(url, callback) {
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        request.onreadystatechange = doNothing;
        callback(request, request.status);
      }
    };

    request.open('GET', url, true);
    request.send(null);
  }

  function doNothing() {}

</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=myapikey&callback=initMap">
</script>

(下面的代码顶部是用于创建标记的XML文件。将XML数据复制到文件中,并命名为 test1.xml )  任何想法如何?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以按照此Polyline doc中的示例开始,可以在地图上画一条连接不同位置的线。

这是JSFiddle中的示例,您可以检查该示例,其中显示了3条由折线连接的位置。

  var flightPlanCoordinates = markerPoints; //the coordinates of your markers
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates, //set the path of the polyline to the marker positions
    geodesic: true, //for the polyline to follow the curvature of the earth
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 3,
    icons: [
      {icon: lineSymbol, offset: '25%'},
      {icon: lineSymbol,  offset: '50%'},
      {icon: lineSymbol, offset: '75%'},
      {icon: lineSymbol, offset: '100%'},
    ]
  });

请在示例小提琴中查看完整代码。希望对您有帮助!