【问题标题】:Implementation Google Maps Polyline and marker together实现谷歌地图折线和标记在一起
【发布时间】:2019-07-18 14:21:55
【问题描述】:

您好,我有一个代码,我可以在地图上看到纬度和经度标记,该代码是我在 tutorial 之后所做的,我想知道如何使每个标记与折线链接

这就是我所拥有的

这就是我想要的

接下来我留下我的代码

    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) 知道怎么做吗?谢谢。

【问题讨论】:

    标签: google-maps-markers


    【解决方案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%'},
        ]
      });
    

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

    【讨论】:

      猜你喜欢
      • 2015-08-05
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2016-08-18
      • 2019-08-02
      • 2012-01-29
      • 1970-01-01
      相关资源
      最近更新 更多