Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在Google地图上绘制多段线_Javascript_Google Maps Api 3 - Fatal编程技术网

Javascript 无法在Google地图上绘制多段线

Javascript 无法在Google地图上绘制多段线,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在尝试在我的页面上动态呈现一条多段线。下面是我的javascript函数,它打算这样做: function decodePath(){ var pathArray=[]; window.alert(("here!!!")); pathArray=[ new google.maps.LatLng(25.774252, -80.190262), new google.maps.LatLng(18.466465, -66.118292), new google.maps.L

我正在尝试在我的页面上动态呈现一条多段线。下面是我的javascript函数,它打算这样做:

function decodePath(){
var pathArray=[];
window.alert(("here!!!"));
pathArray=[
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.75737)
];

var flightPath = new google.maps.Polyline({
path: pathArray,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
poly = new google.maps.Polyline(flightPath);
poly.setMap(map);
}       
附加上述功能的按钮为:

<input type="button" onclick="decodePath();" value="Decode Path"/>

我研究过谷歌地图API,特别是我尝试的是基于示例的。只有我想动态添加多段线。那么为什么路径没有显示在地图上呢?

地图没有在多段线上居中,所以看不到它。将下面的代码添加到“DecodePath”函数中,以使地图在多段线上居中

  var bounds = new google.maps.LatLngBounds();
  for (var i=0; i<pathArray.length;i++){
    bounds.extend(pathArray[i]);
  }
  map.fitBounds(bounds);
var-bounds=new google.maps.LatLngBounds();

对于(var i=0;i代表我。当你说“路径不呈现”时,你的意思是什么?@geocodezip你的代码与我的代码不同。例如,行:。关于“路径不呈现”,我编辑了我的问题。无论如何,感谢你的回复!!:)这是一个分析跟踪程序,对页面的操作没有影响。。。你的地图在显示吗?你的地图是否有尺寸(在你张贴的代码之外)?它是否以多段线为中心?您使用的浏览器是什么?发布的代码中没有错误。1。您没有发布完整的代码,但一直说工作示例与您的(大部分是)2不一样。您的代码没有问题,多段线正在渲染,您在地图上找的位置不正确。非常感谢!!这真的帮了大忙!:)
function initialize() {
  var rendererOptions = {
    draggable: true
  };    

  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom:7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: chicago
  }
  map = new google.maps.Map(document.getElementById('map'), mapOptions);

}
google.maps.event.addDomListener(window, 'load', initialize);
  var bounds = new google.maps.LatLngBounds();
  for (var i=0; i<pathArray.length;i++){
    bounds.extend(pathArray[i]);
  }
  map.fitBounds(bounds);