无法将经度/纬度结果从PHP MySQL查询返回到Javascript JSON [英] Unable to return latitude/longitude result to Javascript JSON from PHP MySQL query

查看:96
本文介绍了无法将经度/纬度结果从PHP MySQL查询返回到Javascript JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to return database values for latitude and longitude from a PHP/MySQL query to my Javascript function in order to populate a google maps JSON request.

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

My PHP/MySQL query script, phpsearch2.php is:

<?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 '}';    
}
?>

它以这种格式返回值:

It returns a value in this format:

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

这里是我的计算根函数,当我运行该程序时,出现一个错误,指出原点未定义,即使我认为我将其设置为具有

Here is my calculate root function, when I run the program I get an error saying 'origin is not defined' even though I think I am setting it to have the same value as the return result from the JSON request to phpsearch, I just don't know where the mistake lies.

 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>';

      } 
      }
    });
 }


推荐答案

作为一个字符串。您需要传递不带引号的结果对象:

You are passing 'result' in as a string. You need to pass the result object, without the quotes:

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

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

Keep in mind that $.getJSON is asynchronous. You probably need to wrap the rest of your gmaps code in a function declaration, and call that function in the $.getJSON callback after origin is set. Otherwise you're still trying to use origin before it has been fetched from the server.

类似于:

Something like:

$.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>
}

这篇关于无法将经度/纬度结果从PHP MySQL查询返回到Javascript JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆