Javascript 谷歌地图编码多段线渲染错误

Javascript 谷歌地图编码多段线渲染错误,javascript,php,google-maps,google-maps-api-3,Javascript,Php,Google Maps,Google Maps Api 3,我在为我的大学在谷歌地图上绘制多段线时遇到问题。结果应该像上面的多段线:(谷歌地图从利兹到伦敦的方向) 我正在从Google directions API渲染每个步骤中的各个路径,而不是仅仅获取Google directions贴图,这样我就可以使用循环来渲染Google directions API中每个步骤中的各个路径以及Rome2Rio搜索API结果中的多段线路径 以下php写出了映射的javascript: <script type="text/javascript"> fu

我在为我的大学在谷歌地图上绘制多段线时遇到问题。结果应该像上面的多段线:(谷歌地图从利兹到伦敦的方向)

我正在从Google directions API渲染每个步骤中的各个路径,而不是仅仅获取Google directions贴图,这样我就可以使用循环来渲染Google directions API中每个步骤中的各个路径以及Rome2Rio搜索API结果中的多段线路径

以下php写出了映射的javascript:

<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 as $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 as $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 as $segment):
    echo 'route'.$segementCounter.'.setMap(map);';
    ++$segementCounter;
endforeach;?>

}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="routeMap" class="floatLeft">
</div>

函数初始化(){
var mylatng=new google.maps.LatLng(53.79649000000006,-1.5473400000000002);
变量myOptions={
缩放:8,
中心:myLatLng,
mapTypeId:google.maps.mapTypeId.TERRAIN
};
var map=new google.maps.map(document.getElementById(“routeMap”),myOptions);

编码的
路径中的反斜杠必须用另一个反斜杠转义:

使用
json\u encode
应保留原始字符串(假设反斜杠已在原始字符串中转义):


我太接近了!谢谢:)尝试使用json_encode传输字符串(见上文)
echo 'var routePath'.$segementCounter.' = '.json_encode($segment->path).';';