我不知道如何将PHP与JSON JavaScript代码一起使用。我假设我需要它来将每一行解析为一个主数组,该数组为每行分配一个从1到无穷大的唯一值键。但是,说实话,我已经看过了,但我不知道该怎么做。另外,如果您发现映射添加标记有任何问题,请告诉我。

PHP代码:

 <?php
 include 'dbconnect.php';

 $result = mysql_query("SELECT * FROM coords ORDER BY name DESC") or die ("Could
 not query");
 while($row = mysql_fetch_array($result)) {
 $r[] = array(
  "name" => $row['name'],
  "lat" => $row['lat'],
  "lng" => $row['lng'],
  "speed" => $row['speed'],
  "altitude" => $row['altitude'],
   "distance" => $row['distance']
);
}

$encoded = json_encode($r);
echo $encoded;

exit($encoded);

mysql_close($conn);

?>


JAVASCRIPT代码:

  var usermarker;
    var markloc;

    function deleteUserOverlay() {
        if (usermarker) {
            usermarker.setMap(null);
        }
    }

    function calluserlocation(){
     console.log('calluserlocation fires');
$.ajax( {
url: "getdata.php",
type: "GET",
dataType: "json",
success: function(data) { for (var i = 0; i < data.length; i++) { markloc = new google.maps.LatLng(data[i].b, data[i].c); adddata(markloc); } }, error: function(data) { console.log( "error" ); } });
console.log("sucessful run of function");

}
   function adddata(markloc){
    marker = new google.maps.Marker({
    position: markloc,
     icon: 'http://www.wolfdoginfo.net/app/cropcircles.png',
    map: map
  });
deleteUserOverlay();
usermarker = marker;
   }


我在控制台中收到错误,我父亲现在这样输出

[{{“ name”:“ test2”,“ lat”:“ 39.8441792”,“ lng”:“-105.104921”,“ speed”:“ bad”,“ altitude”:“ dontcare”,“ distance”:“ whatever” },{“ name”:“ test”,“ lat”:“ 39.729431999999996”,“ lng”:“-104.831919”,“ speed”:“ speed”,“ altitude”:“ altitude”,“ distance”:“ distance “},{” name“:” grant3“,” lat“:” 39.729431999999996“,” lng“:”-104.831919“,” speed“:”速度“,” altitude“:” altitude“,” distance“:” distance“},{” name“:” grant2“,” lat“:” test34“,” lng“:” test34“,” speed“:” speed“,” altitude“:” altitude“,” distance“:” distance“},{” name“:” grant“,” lat“:” 39.729431999999996“,” lng“:”-104.831919“,” speed“:” speed“,” altitude“:” altitude“,” distance“: “ distance”},{“ name”:“”,“ lat”:“ 39.75198511”,“ lng”:“-104.85021166”,“ speed”:“ speed”,“ altitude”:“ altitude”,“ distance”: “ distance”}] [{“ name”:“ test2”,“ lat”:“ 39.8441792”,“ lng”:“-105.104921”,“ speed”:“坏”,“ altitude”:“ dontcare”,“距离“:”“无论”},{“ name”:“ test”,“ lat”:“ 39.729431999999996”,“ lng”:“-104.831919”,“ speed”:“速度”,“ altitude”:“ altitude”,“ distance“:” distance“},{” name“:” grant3“,” lat“:” 39.729431999999996“,” lng“:”-1 04.831919“,” speed“:”速度“,” altitude“:” altitude“,” distance“:” distance“},{” name“:” grant2“,” lat“:” test34“,” lng“:” test34“,” speed“:”速度“,” altitude“:” altitude“,” distance“:” distance“},{” name“:” grant“,” lat“:” 39.729431999999996“,” lng“:” -104.831919“,” speed“:”速度“,” altitude“:” altitude“,” distance“:” distance“},{” name“:”“,” lat“:” 39.75198511“,” lng“:” -104.85021166”,“速度”:“速度”,“高度”:“高度”,“距离”:“距离”}]

所以现在我的php正确输出了两次
但我的json代码无法正常工作。我需要为每个条目填充标记。

结论:

[01:57:52.768] GET http://wolfdoginfo.net/app/show/getdata.php [HTTP/1.1 200 OK 86ms]
[01:57:52.705] "calluserlocation fires"
[01:57:52.705] "sucessful run of function"
[01:57:52.826] "error"

最佳答案

看起来您的PHP代码正在返回无效的JSON,因为它是一系列json字符串,而不是一个组合数组。

while($row = mysql_fetch_array($query)) {
    echo json_encode(array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
// this code will return this JSON
// { a:?, b:?, c:?, ... }
// { a:?, b:?, c:?, ... }
// ...


您应该将它们组合成一个数组并将其返回。

$return = array();
while($row = mysql_fetch_array($query)) {
    $return [] = array( "a" => $row['name'], "b" => $row['lat'], "c" => $row['lng'], "d" => $row['speed'], "e" => $row['altitude'], "f" => $row['distance']));
}
echo json_encode($return);
// this code will return
// [
//    { a:?, b:?, c:?, ... },
//    { a:?, b:?, c:?, ... },
//    ...
// ]


这是一个对象数组,而不仅仅是散乱的对象。

希望这可以帮助!

10-08 07:06