0

我正在尝试使用从 MySQL 检索到的 lat/lng 值来创建用于 GMaps API 的航点数组。我有下面的代码,但我有限的 javascript 知识正在努力将检索到的数据推送到 javascript 数组中以定义为航点数据。

我已经查看了一些在线示例,到目前为止,我已经设法获得了一个解析脚本来毫无问题地检索数据,并在我实例化了地图的页面中调用它:

makeRequest('parsedata.php', function(data) {

            var data = JSON.parse(data.responseText);

            for (var i = 0; i < data.length; i++) {
                displayLocation(data[i]);
            }

        });

parsedata.php 代码:

<?php     
    include 'session.php';

   $query = "SELECT itinerary_link.itineraryID, itinerary_link.coursesID, itinerary_courses.coursename, 
courses.lat, courses.lng FROM itinerary_link LEFT JOIN
 itinerary_courses ON itinerary_link.coursesID = itinerary_courses.coursesID LEFT JOIN 
courses ON courses.coursename = itinerary_courses.coursename WHERE itineraryID=6 ORDER BY coursename";
   $result = mysqli_query($dbc, $query);
   $rows = array();
   while ($r = mysqli_fetch_assoc($result)) {
       $rows[] = $r;
   }
   print json_encode( $rows );
?>

并从此示例输出:

[{"itineraryID":"6","coursesID":"20","coursename":"Carnoustie Championship Course","lat":"56.497414","lng":"-2.720531"},{"itineraryID":"6","coursesID":"21","coursename":"Troon Old Course","lat":"55.534203","lng":"-4.642833"}]

基本上,我无法弄清楚如何操作此输出以创建所需的 lat/lngs javascript 数组,以作为我在页面上运行的路线服务实例的航路点。

与往常一样,任何指针都非常感谢。干杯。

4

1 回答 1

0
    //create an array for the waypoints
  var waypoints=[];

    //only up to 8 waypoints are allowed without business-license
  for(var i=0;i<8 && i<data.length;++i) {

      //push a LatLng-object to the array
    waypoints.push(new google.maps.LatLng(data[i].lat,data[i].lng));
  }
    //use the waypoints
  //doSomethingWith(waypoints);
于 2013-02-18T00:51:55.130 回答