繁体   English   中英

使用数据库中的多个纬度和经度值在Google地图上标记路线

[英]Marking route on Google map with multiple latitude and longitude values from database

我已经编写了PHP和JavaScript代码,它将从数据库中检索纬度和经度信息,并根据适当的位置(使用lat和lon值)在Google地图上放置标记。 这很好。

我需要加入这些标记并在地图上绘制路线。 怎么做。

   <html>
   <head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
   <style type="text/css">
   body { font: normal 10pt Helvetica, Arial; }
    #map { width: 350px; height: 300px; border: 0px; padding: 0px; }
    </style>
   <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
   <script type="text/javascript" src="js/jquery.js"></script>
   <script type="text/javascript">

   var icon = new    google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
   new google.maps.Size(32, 32), new google.maps.Point(0, 0),
   new google.maps.Point(16, 32));
   var center = null;
   var map = null;
   var currentPopup;
   var bounds = new google.maps.LatLngBounds();

  function addMarker(lat, lng, info)
 {
   var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker(
{
    position: pt,
    icon: icon,
    map: map
});
var popup = new google.maps.InfoWindow(
{
    content: info,
    maxWidth: 300
});

google.maps.event.addListener(marker, "click", function()
{
    if (currentPopup != null)
    {
        currentPopup.close();
        currentPopup = null;
    }
    popup.open(map, marker);
    currentPopup = popup;
   });
    google.maps.event.addListener(popup, "closeclick", function()
   {
    map.panTo(center);
    currentPopup = null;
   });
   }

function initMap()
{
 map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions:
{
    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.SMALL
 }
 });


  $.getJSON('googlescript.php', function(items)
  {
   for (var i = 0; i < items.length; i++) {
      (function(item) {
        addMarker(item.lat, item.long, '<b>' + item.name + '</b><br />' + item.desc);
    })(items[i]);
   }
  });

  center = bounds.getCenter();
 map.fitBounds(bounds);

   }
   </script>
    </head>
   <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
   <div id="map"></div>
    </html>

将您的JSON返回函数修改为以下形式:

$.getJSON('googlescript.php', function(items)
  {
   var routePoints = [];
   for (var i = 0; i < items.length; i++) {
      (function(item) {
        addMarker(item.lat, item.long, '<b>' + item.name + '</b><br />' + item.desc);
    })(items[i]);
    routePoints.push(new google.maps.LatLng(items[i].lat,items[i].long));
   }
   var route= new google.maps.Polyline({
    path: routePoints,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  route.setMap(map);

  });

我认为您正在寻找Polyline对象,并且可以看到如何在Google Maps文档中使用

如果您有更大的项目,我可以建议使用.kml文件使用gmap2。 但是您应该可以轻松使用Google文档来实现自己的目标。

您将需要了解有关POLYLINE ARRAYS的知识,以完成所有标记与一条线的连接。 该文档对此进行了很好的解释。

然而,多段线阵列使用JavaScript阵列,因此,如果您正在寻找获取的lat lng从数据库值,那么你应该寻找一些方式来纬度经度的获取来自数据库的数组传递给JavaScript函数。 该网站上已经有一篇文章介绍如何与Google Maps一起使用PHP / MySQL 查看文章并学习使用折线数组,这对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM