简体   繁体   English

谷歌地图编码折线渲染错误

[英]Google Maps Encode Polyline Rendering Wrong

I am having problems rendering polylines on a Google map for my univer. 我在为我的univer在Google地图上渲染折线时遇到问题。 The result should look like the polyline on: http://goo.gl/U18jBJ (Google maps directions from Leeds to London). 结果应该看起来像折线: http//goo.gl/U18jBJ (Google映射从利兹到伦敦的路线)。

I am rendering individual paths from each step from the Google directions API instead of just getting a Google directions map, so that I can use a loop to render the individual paths from each step from the Google directions API and the polyline paths in the Rome2Rio Search API result. 我正在从Google Directions API的每个步骤渲染单独的路径,而不仅仅是获取Google路线图,这样我就可以使用循环来渲染来自Google Directions API的每一步的各个路径以及Rome2Rio搜索中的折线路径API结果。

The following php writes out the javascript for the map: 以下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>

From my research, it seems that it is the javascript which is causing problems (possibly by escaping characters) but I cannot find a resolution. 从我的研究来看,它似乎是导致问题的javascript(可能通过转义字符),但我找不到解决方案。 The code output by the php is here: http://jsfiddle.net/phily245/ebbCX/ php输出的代码如下: http//jsfiddle.net/phily245/ebbCX/

The backslashes in the encoded path must be escaped with another backslash: 必须使用另一个反斜杠转义编码path反斜杠:

http://jsfiddle.net/doktormolle/ebbCX/2/ http://jsfiddle.net/doktormolle/ebbCX/2/

using json_encode should preserve the original string(assuming that the backslashes have been escaped in the original string): 使用json_encode应保留原始字符串(假设反斜杠已在原始字符串中转义):

echo 'var routePath'.$segementCounter.' = '.json_encode($segment->path).';';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM