在谷歌地图标记数组上获取 onclick 函数
我有一个问题。 我一直在寻找一种在标记上获取 onclick
函数的方法。 该标记由 Position 数组组成。 但它似乎没有产生 onclick
功能,即使它做到了,它也只适用于最后制作的标记。
请问你能帮我吗?
这是数组
var POIArrayVisited = new Array(
new Array(52.3764, 4.90245, "De Schreierstoren", "POIone"),
new Array(52.3727, 4.90036, "De Waag", "POItwo"),
new Array(52.3737, 4.90012, "Het Zustersklooster", "POIthree"),
new Array(52.3750, 4.89939, "Onze lieve heer op solder", "POIfour"),
new Array(52.3741, 4.89808, "Belle het standbeeld", "POIfive"));
然后我创建标记:
// voer de coordinaten van de niet bezochte poi in
// zet markers voor elk POI
var i = 0;
for (i = 0; i < POIArrayVisited.length; i++) {
var markerLatlng = new google.maps.LatLng(
POIArrayVisited[i][0], POIArrayVisited[i][1])
// Place a hit marker
markerVisited = new google.maps.Marker({
position: markerLatlng,
map: map,
icon: imageMarkerOld,
title: POIArrayVisited[i][2]
});
}
然后它将创建 onclick
标记。
// For every POI
var i;
for (i = 0; i < POIArrayVisited.length; i++) {
var POIlinkVisited = POIArrayVisited[i][3];
var OpenPOI = POIlinkVisited;
google.maps.event.addListener(markerVisited, "click", function() {
//link and update cookie
document.cookie = "OpenPOI" + "=" + OpenPOI;
window.location.href = "poi.php";
});
}
我不明白我做错了什么
I have a question.
I've been searching for a way to get an onclick
function on a marker.
This marker is made by a Position array.
But it seems it doesn't make the onclick
function and if it does it only works on the last made marker.
Please can you help me ?
This is the array
var POIArrayVisited = new Array(
new Array(52.3764, 4.90245, "De Schreierstoren", "POIone"),
new Array(52.3727, 4.90036, "De Waag", "POItwo"),
new Array(52.3737, 4.90012, "Het Zustersklooster", "POIthree"),
new Array(52.3750, 4.89939, "Onze lieve heer op solder", "POIfour"),
new Array(52.3741, 4.89808, "Belle het standbeeld", "POIfive"));
then I create the marker:
// voer de coordinaten van de niet bezochte poi in
// zet markers voor elk POI
var i = 0;
for (i = 0; i < POIArrayVisited.length; i++) {
var markerLatlng = new google.maps.LatLng(
POIArrayVisited[i][0], POIArrayVisited[i][1])
// Place a hit marker
markerVisited = new google.maps.Marker({
position: markerLatlng,
map: map,
icon: imageMarkerOld,
title: POIArrayVisited[i][2]
});
}
and then it will create the onclick
marker.
// For every POI
var i;
for (i = 0; i < POIArrayVisited.length; i++) {
var POIlinkVisited = POIArrayVisited[i][3];
var OpenPOI = POIlinkVisited;
google.maps.event.addListener(markerVisited, "click", function() {
//link and update cookie
document.cookie = "OpenPOI" + "=" + OpenPOI;
window.location.href = "poi.php";
});
}
I don't get what I'm doing wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用数组并使用链接或 ID 来定义需要在其他页面上打开的内容,您可以使用此
首先得到带有
纬度和经度的 数组
然后是一个标题
以及您要使用的值的 ID。
这样你就可以使用这个代码来制作一个 cookie
,这样你就可以将它用作 php 的 ID
或者你想引用一个 html 页面
感谢大家的帮助! =D
If you want to use an array and use a link or ID to define what is needs to open on a other page you can use this
First get you array with
Lat and Long
Then a title
and the ID of value you want to use.
This way you can use this code to make a cookie
and with this you can use it as an ID for php
OR is you want to refer to a html page
Thanks for a the help guys !! =D
在您的代码中:
您从哪里获取
markerVisited
的新实例?据我在您发布的代码片段中看到,您正在循环访问 POIArrayVisited,但您没有获取 markerVisited 的新实例。因此,这可能就是只有 LAST 标记实际响应点击的原因。应该类似于:
In your code:
Where do you get a new instance of
markerVisited
? As far as I can see in the code snippets you posted you are cycling through thePOIArrayVisited
but you are not obtaining a new instance of themarkerVisited
. So that's probably the reason only the LAST marker actually responds on the click.Should be something similar to: