我必须用小工具制作地图,这些小工具从上一点开始分别获取电压。
我的数据库中有lat和lng记录。我用自定义图标和windowInfo在地图上存储带有标记的小工具,但我想获取上一点的经纬度,并在上一点和新点之间建立折线。
我怎样才能做到这一点 ?

这是我的索引代码

var customIcons = {
  контрагент: {
    icon: 'img/contragent.png'
  },
  switch_16p: {
    icon: 'img/switch_16p.png'
  },
  switch_8p: {
    icon: 'img/switch_8p.png'
  },
  switch_6p:{
    icon: 'img/switch_6p.png'
  }
};

function load(select) {


  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(43.8496, 25.9552),
    zoom: 14,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;



  // Change this depending on the name of your PHP file
  downloadUrl("settings_map.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var opis_id = markers[i].getAttribute("opis_id");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var kvartal = markers[i].getAttribute("kvartal");
      var gadget = markers[i].getAttribute("gadget");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>Опис ID</b> - " + opis_id + "<br/> <br/><b>Адрес:</b> " + address + "<br/><b>Квартал:</b> " + kvartal + "<br><b>У-во: </b>" + gadget;


      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon
      });

      bindInfoWindow(marker, map, infoWindow, html);

    }
  });
}



function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {

    infoWindow.setContent(html);
    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() {

}


        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: {lat: 0, lng: -180},
          mapTypeId: 'terrain'
        });

最佳答案

定义一个数组以跟踪已经添加到地图的标记:




var gmarkers = [];



在第一个创建折线之后,将标记添加到该数组:




gmarkers.push(marker);
if (i > 0) {
  var polyline = new google.maps.Polyline({
    path: [gmarkers[i - 1].getPosition(), marker.getPosition()],
    map: map
  });
}


proof of concept fiddle

代码段:



var map;
var gmarkers = [];

function load() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(43.8496, 25.9552),
    zoom: 14,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;
  var xml = parseXml(xmlStr);
  var markers = xml.documentElement.getElementsByTagName("marker");
  var bounds = new google.maps.LatLngBounds();
  for (var i = 0; i < markers.length; i++) {
    var opis_id = "opis_id";
    var address = markers[i].getAttribute("address");
    var type = markers[i].getAttribute("type");
    var kvartal = "kvartal";
    var gadget = "gadget";
    var point = new google.maps.LatLng(
      parseFloat(markers[i].getAttribute("lat")),
      parseFloat(markers[i].getAttribute("lng")));
    bounds.extend(point);
    var html = "<b>Опис ID</b> - " + opis_id + "<br/> <br/><b>Адрес:</b> " + address + "<br/><b>Квартал:</b> " + kvartal + "<br><b>У-во: </b>" + gadget;

    var marker = new google.maps.Marker({
      map: map,
      position: point,
    });
    bindInfoWindow(marker, map, infoWindow, html);
    gmarkers.push(marker);
    if (i > 0) {
      var polyline = new google.maps.Polyline({
        path: [gmarkers[i - 1].getPosition(), marker.getPosition()],
        map: map
      });
    }
  }
  map.fitBounds(bounds);
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

function parseXml(str) {
  if (window.ActiveXObject) {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  } else if (window.DOMParser) {
    return (new DOMParser).parseFromString(str, 'text/xml');
  }
}
google.maps.event.addDomListener(window, 'load', load);

var xmlStr = '<?xml version="1.0"?><markers><marker name="Round Table Pizza: Mountain View" address="570 N Shoreline Blvd, Mountain View, CA" lat="37.402653" lng="-122.079353" distance="0.38091455044131"/><marker name="Kapp\'s Pizza Bar &amp; Grill" address="191 Castro St, Mountain View, CA" lat="37.393887" lng="-122.078918" distance="0.5596115438175"/><marker name="Amici\'s East Coast Pizzeria" address="790 Castro St, Mountain View, CA" lat="37.387138" lng="-122.083237" distance="1.0796074495809"/><marker name="Frankie Johnnie &amp; Luigo Too" address="939 W El Camino Real, Mountain View, CA" lat="37.386337" lng="-122.085823" distance="1.2044231336188"/><marker name="Tony &amp; Alba\'s Pizza &amp; Pasta" address="619 Escuela Ave, Mountain View, CA" lat="37.394012" lng="-122.095528" distance="1.3156538737837"/><marker name="Round Table Pizza: Sunnyvale-Mary-Central Expy" address="415 N Mary Ave, Sunnyvale, CA" lat="37.390038" lng="-122.042030" distance="1.84565061776"/><marker name="Oregano\'s Wood-Fired Pizza" address="4546 El Camino Real, Los Altos, CA" lat="37.401726" lng="-122.114647" distance="2.2887425990519"/><marker name="Pizz\'a Chicago" address="4115 El Camino Real, Palo Alto, CA" lat="37.414104" lng="-122.126221" distance="3.0702714925465"/><marker name="California Pizza Kitchen" address="531 Cowper St, Palo Alto, CA" lat="37.448074" lng="-122.158813" distance="5.744901789628"/><marker name="Patxi\'s Chicago Pizza" address="441 Emerson St, Palo Alto, CA" lat="37.445148" lng="-122.163551" distance="5.8508447055485"/><marker name="Applewood Pizza 2 Go" address="1001 El Camino Real, Menlo Park, CA" lat="37.452965" lng="-122.181725" distance="6.9830185240251"/><marker name="Fiorillo\'s Restaurant" address="638 El Camino Real, Santa Clara, CA" lat="37.354603" lng="-121.942574" distance="7.8305245099868"/><marker name="Round Table Pizza" address="4302 Moorpark Ave, San Jose, CA" lat="37.315903" lng="-121.977928" distance="7.8357853019702"/><marker name="Tony &amp; Alba\'s Pizza &amp; Pasta" address="3137 Stevens Creek Blvd, San Jose, CA" lat="37.323296" lng="-121.951645" distance="8.534766437601"/><marker name="Pizza Antica" address="334 Santana Row # 1065, San Jose, CA" lat="37.321793" lng="-121.947739" distance="8.7673701809137"/><marker name="Amici\'s East Coast Pizzeria" address="225 W Santa Clara St, San Jose, CA" lat="37.334702" lng="-121.894043" distance="10.826482447706"/><marker name="Pizz\'a Chicago" address="155 W San Fernando St, San Jose, CA" lat="37.333279" lng="-121.891678" distance="10.985777935382"/><marker name="House of Pizza" address="527 S Almaden Ave, San Jose, CA" lat="37.326351" lng="-121.888168" distance="11.368626024055"/><marker name="Amici\'s East Coast Pizzeria" address="226 Redwood Shores Pkwy, Redwood City, CA" lat="37.520515" lng="-122.252258" distance="12.8643535655"/></markers>';

html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

关于javascript - 如何使用start Latlng和End Latlng在数据库的所有标记之间创建折线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41382917/

10-17 01:28