php - Cannot populate JSON request with correctly formatted data -
i trying populate json request data coming phpsearch.php file (shown here)
<?php include "base.php"; $name = $_get["name"]; $query = "select lat, lng markers name = '".$name."'"; $result = mysql_query($query); $json = array(); while($row = mysql_fetch_array ($result)) { $bus = array( 'lat' => $row['lat'], 'lng' => $row['lng'] ); array_push($json, $bus); } $jsonstring = json_encode($json); echo $jsonstring; ?>
the data shows in console in format:
[{"lat":"37.730267","lng":"-122.498589"}]
the route calculation function @ bottom of question, had used asynchronous json rewquest causing code execjte before origin value set, being set looks incorrect
how can make sure latitude , longitude results in correct json format? json request looks this:
request url:https://maps.googleapis.com/maps/api/js/directionsservice.route?4b0&5m4&1m3&1m2&1dnan&2dnan&5m4&1m3&1m2&1d37.738029&2d-122.499481&6e1&8b1&12sen-gb&100b0&102b0&callback=_xdc_._1rqnjk&token=80599
route calculation code:
function calcroute() { var startname = document.getelementbyid('start').value; var endname = document.getelementbyid('end').value; var waypts = []; var checkboxarray = document.getelementbyid('waypoints'); (var = 0; < checkboxarray.length; i++) { if (checkboxarray.options[i].selected == true) { waypts.push({ location:checkboxarray[i].value, stopover:true}); } } $.ajax({ url:'phpsearch2.php', datatype:'html', data:{name:startname}, async:false, success:function (result) { console.log(result) origin = new google.maps.latlng(result[0].lat, result[0].lng); }}); 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>'+ origin +'</b>'); if (status == google.maps.directionsstatus.ok) { directionsdisplay.setdirections(response); var route = response.routes[0]; var summarypanel = document.getelementbyid('directions_panel'); summarypanel.innerhtml = ''; // each route, display summary information. (var = 0; < route.legs.length; i++) { var routesegment = + 1; summarypanel.innerhtml += '<b>time walkabout </b><br>'; summarypanel.innerhtml += '<b>from ' + startname + ' </b>'; summarypanel.innerhtml += '<b>to ' + endname + '('+ route.legs[i].distance.text +')</b><br>'; } } }); }
the latlng constructor takes 2 numbers arguments, you're passing json object. latitude , longitude properties out , pass numbers directly, this:
function (result){ origin = new google.maps.latlng(result[0].latitude, result[0].longitude); }
i'm guessing on specific properties, may want console.log(result)
see exact object structure.
Comments
Post a Comment