区块链技术博客
www.b2bchain.cn

如何使用start Latlng和End Latlng在数据库的所有标记之间创建折线? – javascript程序员分享

本文介绍了如何使用start Latlng和End Latlng在数据库的所有标记之间创建折线? – javascript程序员分享,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

我必须用小工具制作地图,这些小工具从上一点开始分别获取电压。
我的数据库中有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>

如何设置Telerik单选按钮所需的字段验证器?我想在按钮单击“ BtnSave”上设置必填字段验证器吗?请帮忙!<telerik:RadButton ID="radio_male" runat="server" ToggleType="Radio" AutoPostBack="fa…

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…

我需要在我的php中调用js函数,但无法正常工作。有人可以告诉我我在做什么错吗?我该如何轻松地做到这一点?谢谢!我有三个文件:  mail.php负责发送$ _POST的内容(工作正常)。我调用我的javascript函数来切换模式,具体取决于邮件是否已发送。 <? … $response = $sendgrid->send($email);…

            当表单由onchange事件提交时,它不会保留get变量view。任何想法为什么会发生这种情况?提交后,这是它进入的网址,index?month=February&year=2014<form action="index?view=list" class="pure-form pure-fo…

我有一个表格,其中取决于用户的选择,输入元素是否可见。实际上,用户正在以另一种形式设置已定义的合作伙伴类型,并且如果选中该元素,则允许在该类型的合作伙伴上可见的元素类型1将显示以下元素:<input type="text" id="partner" name="partner" class=&…

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 如何使用start Latlng和End Latlng在数据库的所有标记之间创建折线? – javascript程序员分享
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们