简体   繁体   English

处理JSON数据以在Google Maps API V3中创建经纬度的javascript数组

[英]Manipulating JSON data to create javascript array of lat/lngs in Google Maps API V3

I'm trying to use lat/lng values retrieved from MySQL to create an array of waypoints for use in the GMaps API. 我正在尝试使用从MySQL检索到的纬度/经度值来创建在GMaps API中使用的航点数组。 I have the code below but with my limited javascript knowledge am struggling to push this retrieved data into a javascript array to define as the waypoints data. 我有下面的代码,但是由于我有限的javascript知识,我正在努力将检索到的数据推入javascript数组中以定义为航点数据。

I've looked at a few online examples and so far I have managed to get a parsing script to retrieve the data with no problems and call this within the page I have the maps instantiated on: 我看了一些在线示例,到目前为止,我已经设法获得一个解析脚本来毫无问题地检索数据,并在实例化了地图的页面中调用此脚本:

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

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

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

        });

The parsedata.php code: 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 );
?>

And sample output from this: 并从中输出示例:

[{"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"}]

Basically I can't work out how to manipulate this output to create the required javascript array of lat/lngs to feed in as the waypoints for a directions service instance I have running on the page. 基本上,我无法弄清楚如何处理此输出以创建所需的经纬度javascript数组,以作为我在页面上运行的路线服务实例的路标。

As always, any pointers much appreciated. 与往常一样,任何指针都值得赞赏。 Cheers. 干杯。

    //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);

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

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