javascript Google Maps API v3:在新窗口中打开标记上的链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21632094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps API v3: Opening the link on a marker in a new window
提问by Arerrac
seems like a pretty basic thing, but I don't seem to be finding the answer. Here's my Google Maps code:
似乎是一件非常基本的事情,但我似乎没有找到答案。这是我的谷歌地图代码:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(123, 123),
url: "http://www.google.com",
map: map
});
...
...
google.maps.event.addListener(marker, 'click', function() {
window.location.href = marker.url;
});
What do I need to change, to open the link in a new window?
我需要更改什么才能在新窗口中打开链接?
Many thanks!
非常感谢!
回答by sabotero
You have to use the method open
of the window
object like this:
你必须使用的方法open
中的window
这样的对象:
window.open("http://www.w3schools.com");
window.open("http://www.w3schools.com");
So your code will be something like this:
所以你的代码将是这样的:
google.maps.event.addListener(marker, 'click', function() {
window.open(marker.url);
});
Visit this linkfor an overview of the open
method and the options you can set.
请访问此链接,了解该open
方法和您可以设置的选项的概述。
回答by drchuck
So I used this approach but when I was in a loop making lots of markers, each with a different URL, this was the approach I needed to take.
所以我使用了这种方法,但是当我在循环中制作大量标记时,每个标记都有不同的 URL,这是我需要采用的方法。
var newLatlng = new google.maps.LatLng(row[0], row[1]);
var marker = new google.maps.Marker({
position: newLatlng,
map: map,
title: row[2],
url: row[3]
});
google.maps.event.addListener(marker, 'click', function() {
window.open('https://www.youtube.com/watch?v='+this.url);
});
Note that the key was using this.url
in the listener code. If I used marker.url
in the listener code, it would open the last of many urls for all of the markers.
请注意,该密钥this.url
在侦听器代码中使用。如果我marker.url
在侦听器代码中使用,它将打开所有标记的许多 url 中的最后一个。