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.
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>';
}
}
});
}