通过在Google API中从MySql和PHP中获取数据,绘制随着汽车行驶而增长的折线


9

我想通过从MySQL和PHP中获取数据,随着汽车随着动画的运动而在Google Map上显示折线(例如在本网站:http : //econym.org.uk/gmap/example_cartrip2.htm)。为此,我引用了Google API教程Polyline中的代码。我还从SQL中获取了以下数据:.html

function load() {
    var point;
                    var flightPlanCoordinates=new Array();
            var map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(18.33, 73.55),
                zoom: 7,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: false,
                mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
                 },
                navigationControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL
                }
            });

            downloadUrl("xmltry.php", function(data) {
        var xml = data.responseXML;

        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lon")));
            flightPlanCoordinates[i]=point;
          }
          var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  flightPath.setMap(map);

      });

        }

        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() {}

.php文件:

<?php
//require("phpsqlajax_dbinfo.php");
include 'common_db.inc';
// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server

$connection=mysql_connect ("$dbserver", "$dbuser", "$dbpass");
if (!$connection) {  die('Not connected : ' . mysql_error());} 

// Set the active MySQL database

$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
} 

// Select all the rows in the markers table

$query = "SELECT  `lat`, `lon` FROM current_location Where Device_ID='HGS1005'";
$result = mysql_query($query);
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);  
  $newnode->setAttribute("lat", $row['lat']);  
  $newnode->setAttribute("lon", $row['lon']);  
  } 

echo $dom->saveXML();

?>

但是现在我想在第一参考站点中显示带有动画的折线。任何人都可以建议我一些参考或在同一方面帮助我。我得到的结果是正确的,但希望像http://econym.org.uk/gmap/example_cartrip2.htm 中那样具有动画效果,在此处输入图片说明 希望这次我的标题和说明部分能被理解。

Answers:


2

适用于PolylinepathMaps Javascript API文档(请注意属性)

折线的坐标的有序序列。可以使用简单的LatLng数组或MVCArray的LatLngs指定此路径。请注意,如果您传递一个简单的数组,它将被转换为MVCArray。 MVCArray中插入或删除LatLng会自动更新地图上的折线

知道这一点,我会这样尝试:

// I assume you've got your GPS signal pumping updates to your client.
// Insert a new GPS point into your Polyline's point array.
function OnGpsPulse(newLat, newLon)
{
  // Insert a new point at the end of the LatLng Array.
  pointIndex = flightPlanCoordinates.length;      
  flightPlanCoordinates[pointIndex]=new google.maps.LatLng(newLat, newLon);

  // And the line should automatically update in the map.
}

事后考虑,每个GPS脉冲应调度一个您可能需要/想要在两个地方处理的事件。首先,如果要无限期存储车辆的位置,则需要在服务器端收集它们并将其推送到数据库中。但是,如果您只想让车辆在行驶过程中画线,则可以将该功能保留在客户端中。


感谢您的答复,但是,我将在数据库(即MySQL)中存储数据。我不希望实时跟踪,而是想要知道我的车辆移动的历史,为此,我想显示跟踪动画,如本示例(econym.org.uk/gmap/example_cartrip2.htm)。我想向存储的数据添加动画。汽车图像(即png)将在其中移动,并且随着该图像的运动(如第一个链接),车辆轨迹将绘制。
帕里
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.