2017-07-14 83 views
-1

我正在尝试将Google地图嵌入到我的网页中,并使用链接到不同网站的标签。我将如何去做这件事? 供图这是我的源代码至今:嵌入的谷歌地图与链接到网页的针脚

<script type="text/javascript"> 
    var locations = [ 
     ['Bondi Beach', -33.890542, 151.274856, 4, "google.com"], 
     ['Coogee Beach', -33.923036, 151.259052, 5], 
     ['Cronulla Beach', -34.028249, 151.157507, 3], 
     ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
     ['Maroubra Beach', -33.950198, 151.259302, 1] 
    ]; 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 10, 
     center: new google.maps.LatLng(-33.92, 151.25), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map, 
     url: locations[i][3], 

     }); 

     google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     })(marker, i)); 
    } 
    </script> 

回答

0

这里是一个很好的例子:https://gist.github.com/parth1020/4481893

如果要添加链接,然后只需添加URL到阵列,并在for循环中添加URL选项。

代码:

var locations = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4, 'http://link.com/example1'], 
    ['Coogee Beach', -33.923036, 151.259052, 5, 'http://link.com/example2'] 
]; 


for (i = 0; i < locations.length; i++) { 
    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
    map: map, 
    url: locations[i][4], 

    }); 
+0

我仍然不能得到URL时,我上的标记点击工作。请参阅上面编辑过的帖子。 –

+0

将http或https添加到我的末尾并将网址设置为位置[i] [4] – Jack

+0

我不知道我在做什么错误,但该网址仍然无效。这是不正确的?当我评论代码时,http会消失,但链接是“http://google.com”。代码:var locations = [ ['Bondi Beach',-33.890542,151.274856,4,“http://google.com”], ['Coogee Beach',-33.923036,151.259052,5], ['Cronulla Beach',-34.028249,151.157507,3], ['Manly Beach',-33.80010128657071,151.28747820854187,2], ['Maroubra Beach',-33.950198,151.259302,1] –