javascript - Google Maps Encode Polyline Rendering Wrong -
i having problems rendering polylines on google map univer. result should polyline on: http://goo.gl/u18jbj (google maps directions leeds london).
i rendering individual paths each step google directions api instead of getting google directions map, can use loop render individual paths each step google directions api , polyline paths in rome2rio search api result.
the following php writes out javascript map:
<script type="text/javascript"> function initialize() { var mylatlng = new google.maps.latlng(53.796490000000006, -1.5473400000000002); var myoptions = { zoom: 8, center: mylatlng, maptypeid: google.maps.maptypeid.terrain }; var map = new google.maps.map(document.getelementbyid("routemap"), myoptions); <?php $stopcounter = 0; foreach($traveldata[0]->stops $stop): echo 'var stoplatlng'.$stopcounter.' = new google.maps.latlng('.$stop->location.');'; echo 'var marker'.$stopcounter.' = new google.maps.marker({ position: stoplatlng'.$stopcounter.', map: map, title: \''.$stop->name.'\' });'; ++$stopcounter; endforeach; $segementcounter = 0; foreach($traveldata[0]->segments $segment): echo 'var routepath'.$segementcounter.' = \''.$segment->path.'\'; '; echo 'var path'.$segementcounter.' = google.maps.geometry.encoding.decodepath(string(routepath'.$segementcounter.')); '; echo 'var route'.$segementcounter.' = new google.maps.polyline({ path: path'.$segementcounter.', strokecolor: "#ff0000", strokeopacity: 1.0, strokeweight: 2 }); '; ++$segementcounter; endforeach; $segementcounter = 0; foreach($traveldata[0]->segments $segment): echo 'route'.$segementcounter.'.setmap(map);'; ++$segementcounter; endforeach;?> } google.maps.event.adddomlistener(window, 'load', initialize); </script> <div id="routemap" class="floatleft"> </div>
from research, seems javascript causing problems (possibly escaping characters) cannot find resolution. code output php here: http://jsfiddle.net/phily245/ebbcx/
the backslashes in encoded path
must escaped backslash:
http://jsfiddle.net/doktormolle/ebbcx/2/
using json_encode
should preserve original string(assuming backslashes have been escaped in original string):
echo 'var routepath'.$segementcounter.' = '.json_encode($segment->path).';';
Comments
Post a Comment