2013-04-06 80 views
0

我想从PHP/MySQL查询返回数据库的纬度和经度值到我的Javascript函数,以填充谷歌地图JSON请求。无法将经度/纬度结果从PHP返回到Javascript JSON MySQL查询

我的PHP/MySQL查询脚本,phpsearch2.php是:

<?php 
include "base.php"; 
$name = $_GET["name"]; 
$query = "SELECT lat, lng FROM markers WHERE name = '".$name."'"; 
$result = mysql_query($query); 
while($row = mysql_fetch_array ($result))  
{ 
    echo '{'; 
      echo '"latitude":"'.$row['lat'].'",'; 
      echo '"longitude":"'.$row['lng'].'",'; 
    echo '}';  
} 
?> 

它返回这个格式的值:

{"latitude":"37.730267","longitude":"-122.498589",} 

这里是我的根计算功能,当我运行程序我得到一个错误,说'起源未定义'即使我认为我设置它具有相同的价值作为返回结果从JSON请求到phpsearch,我只是不知道错误在哪里。

function calcRoute() { 
    var startname = document.getElementById('start').value; 
    var endname = document.getElementById('end').value; 
    var waypts = []; 
    var checkboxArray = document.getElementById('waypoints'); 
    for (var i = 0; i < checkboxArray.length; i++) { 
     if (checkboxArray.options[i].selected == true) { 
     waypts.push({ 
      location:checkboxArray[i].value, 
      stopover:true}); 
     } 
    } 

$.getJSON("phpsearch2.php", {name : startname}, function (result) { 
origin = google.maps.LatLng('result'); 
}); 

var end = new google.maps.LatLng('37.738029', '-122.499481'); 
    var request = { 
     origin: origin, 
     destination: end, 
     waypoints: waypts, 
     optimizeWaypoints: true, 
     travelMode: google.maps.DirectionsTravelMode.WALKING 
    }; 
     directionsService.route(request, function(response, status) { 
    //document.write('<b>'+ start + end + '</b>'); 
     if (status == google.maps.DirectionsStatus.OK) { 
     directionsDisplay.setDirections(response); 
     var route = response.routes[0]; 
     var summaryPanel = document.getElementById('directions_panel'); 
     summaryPanel.innerHTML = ''; 
     // For each route, display summary information. 
     for (var i = 0; i < route.legs.length; i++) { 
      var routeSegment = i + 1; 
      summaryPanel.innerHTML += '<b>Time for a Walkabout </b><br>'; 
      summaryPanel.innerHTML += '<b>From ' + startname + ' </b>'; 
      summaryPanel.innerHTML += '<b>to ' + endname + '('+ route.legs[i].distance.text +')</b><br>'; 

     } 
     } 
    }); 
} 
+0

OT:始终使用'json_encode'在PHP中生成JSON。看看这个答案,了解同步和异步代码之间的区别:http://stackoverflow.com/a/14220323/218196。 Ajax是异步的。 – 2013-04-07 00:25:31

回答

1

您将'result'作为字符串传入。您需要传递不带引号的结果对象:

var origin = google.maps.LatLng(result); 

请记住$ .getJSON是异步的。您可能需要将其余gmaps代码包装在函数声明中,并在设置了原点后,在$ .getJSON回调中调用该函数。否则,您仍然在从服务器获取源之前尝试使用源。

喜欢的东西:

$.getJSON("phpsearch2.php", {name : startname}, function (result) { 
    var origin = google.maps.LatLng(result); 

    // Now that we have our origin, we can initialize the rest 
    init(origin); 
}); 

function init(origin) { 
    var end = new google.maps.LatLng('37.738029', '-122.499481'); 
    var request = { 
     origin: origin, 
    <snip> 
} 
+0

谢谢,这肯定是一个问题,我仍然看到一个错误我作出这样的改变之后,虽然 错误:财产无效值:未定义 ... Y)/ d); F && f.io &&( qa(k,f.io [0]),Ka(k,f.io [1])); return {ya:f,W:e,df:g,anchorOffset:k ... – 2013-04-06 23:48:31

+0

我更新了我的答案,认为这就是它 – jszobody 2013-04-06 23:51:21

+0

看看更新的答案。在继续前,您需要等待$ .getJSON返回。 – jszobody 2013-04-07 00:07:14