Javascript 从地图标记将Google方向打开到div

Javascript 从地图标记将Google方向打开到div,javascript,html,google-maps,Javascript,Html,Google Maps,我试着在一个div中点击谷歌地图,而不是在一个新窗口中,从一个标记打开方向 目前,我可以在新窗口中打开方向,但我希望在单击标记时显示的div中的iframe中打开方向 我不能在iframe上使用静态src url,因为我对地图上的不同标记使用不同的url 任何帮助都会很好,这就是我目前所拥有的 var depots = [ ['Barnsley', 53.572664, -1.455800, 11, 'http://maps.google.com/?daddr=53.572664,-1.

我试着在一个div中点击谷歌地图,而不是在一个新窗口中,从一个标记打开方向

目前,我可以在新窗口中打开方向,但我希望在单击标记时显示的div中的iframe中打开方向

我不能在iframe上使用静态src url,因为我对地图上的不同标记使用不同的url

任何帮助都会很好,这就是我目前所拥有的

  var depots = [
  ['Barnsley', 53.572664, -1.455800, 11, 'http://maps.google.com/?daddr=53.572664,-1.455800&directionsmode=driving'],
  ['Birmingham', 52.359018, -1.938033, 10, 'http://maps.google.com/?daddr=52.359018,-1.938033&directionsmode=driving'],
  ['Brentwood', 51.576452, 0.278843, 9, 'http://maps.google.com/?daddr=51.576452,0.278843&directionsmode=driving'],
  ['Bristol', 51.501280, -2.366513, 8, 'http://maps.google.com/?daddr=51.501280,-2.366513&directionsmode=driving'],
  ['Cambridge', 52.184396, -0.064012, 7, 'http://maps.google.com/?daddr=52.184396,-0.064012&directionsmode=driving'],
  ['Edinburgh', 56.129890, -3.390296, 6, 'http://maps.google.com/?daddr=56.129890,-3.390296&directionsmode=driving'],
  ['Gatwick', 51.152422, -0.216219, 5, 'http://maps.google.com/?daddr=51.152422,-0.216219&directionsmode=driving'],
  ['Glasgow', 55.927129, -4.467189, 4, 'http://maps.google.com/?daddr=55.927129,-4.467189&directionsmode=driving'],
  ['Heathrow', 51.508121, -0.387525, 3, 'http://maps.google.com/?daddr=51.508121,-0.387525&directionsmode=driving'],
  ['Manchester', 53.220004, -2.414895, 2, 'http://maps.google.com/?daddr=53.220004,-2.414895&directionsmode=driving'],
  ['Southampton', 50.959088, -1.345449, 1, 'http://maps.google.com/?daddr=50.959088,-1.345449&directionsmode=driving'],


  ];
for (var i = 0; i < locations.length; i++) {
    var depot = locations[i];
    var myLatLng = new google.maps.LatLng(depot[1], depot[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: depot[0],
        zIndex: depot[3],
        url: depot[4]
    });

    google.maps.event.addListener(marker, 'click', function() {
        document.getElementById('direction').style.display = "";
        window.location.href = this.url;
    });
}

您是否尝试将其拆分为一个函数

google.maps.event.addListener(marker, 'click', direction);
function direction(evt)
{
   document.getElementById('directioniframe').src = this.url;
}

下面是一个简单的示例,说明如何执行此操作:

我已经粘贴了应该在循环中的代码。看一看

JS:

关于您在评论中提到的错误,请参考以下答案:


错误一定是由于协议问题造成的。还请注意,由于
同源策略
,iframe将不会加载。阅读此处的更多信息:

删除“URLis”中的引号,在最后一行代码中使用URLis,然后重试。我得到一个答案:(文件或目录找不到。在我的iFrame中,“位置”中有什么内容?你能在这里发布吗?用depot var编辑的问题刚刚尝试了没有jou,我应该把URLis var放在哪里?仍然在函数中?你也可以为标记设置一个属性,如“depotIndex”,在子函数中读取它,并构建您的url从depot Array以友好方式读取数据。我使用url,以便当点击标记时,它在地图中有结束位置。我不确定创建depotindex是什么意思?因此,由于属于不同的域,这是不可能的。如果您想了解更多信息,嗯,好的,您能建议吗解决方法?它适用于一个web应用程序,当点击相应的标记时,该应用程序将允许指向该位置的方向。如果我可以打开手机的本机浏览器查看它们,我会很高兴。我确实尝试过让URL方案工作,以便它可以在地图应用程序中打开。您可以尝试在“新建”选项卡中打开URL,并基于用户的设置,即“本机”可能会为url打开地图应用程序。请检查更新的答案
var URLis = this.url;
document.getElementById('directioniframe').src = " URLis ";
google.maps.event.addListener(marker, 'click', direction);
function direction(evt)
{
   document.getElementById('directioniframe').src = this.url;
}
 var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: 'Hello World!',
    url:'http://jsfiddle.net'
});
google.maps.event.addListener(marker, 'click', function () {
    //document.getElementById('direction').style.display = "";
    var win = window.open(this.url, '_blank');
    win.focus();
});