在谷歌地图标记数组上获取 onclick 函数

发布于 2024-12-09 04:28:58 字数 1533 浏览 0 评论 0原文

我有一个问题。 我一直在寻找一种在标记上获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2024-12-16 04:28:58

如果您想使用数组并使用链接或 ID 来定义需要在其他页面上打开的内容,您可以使用此
首先得到带有

纬度和经度的 数组
然后是一个标题
以及您要使用的值的 ID。

    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")
);

这样你就可以使用这个代码来制作一个 cookie

// 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            document.cookie = "OpenPOI"+"="+this.html;
                            window.location.href = "poi.php";
                    });
                }

,这样你就可以将它用作 php 的 ID

                    // 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            window.location.href = "poi.php?id="+this.html;
                    });
                }

或者你想引用一个 html 页面

                // 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            window.location.href = +this.html".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.

    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")
);

This way you can use this code to make a cookie

// 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            document.cookie = "OpenPOI"+"="+this.html;
                            window.location.href = "poi.php";
                    });
                }

and with this you can use it as an ID for php

                    // 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            window.location.href = "poi.php?id="+this.html;
                    });
                }

OR is you want to refer to a html page

                // 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],
                        html: POIArrayVisited[i][3]
                    });
                    var OpenPOIVisited = POIArrayVisited[i][3];
                        google.maps.event.addListener(markerVisited, "click", function() {
                            //link and update cookie
                            window.location.href = +this.html".html";
                    });

Thanks for a the help guys !! =D

知你几分 2024-12-16 04:28:58

在您的代码中:

// 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";
    });

}

您从哪里获取 markerVisited 的新实例?据我在您发布的代码片段中看到,您正在循环访问 POIArrayVisited,但您没有获取 markerVisited 的新实例。因此,这可能就是只有 LAST 标记实际响应点击的原因。

应该类似于:

for (var i = 0; i < POIArrayVisited.length; i++) {
    var markerLatlng = new google.maps.LatLng(POIArrayVisited[i][0], POIArrayVisited[i][1]);
    // Place a hit marker
    var markerVisited = new google.maps.Marker({
        position: markerLatlng,
        map: map,
        icon: imageMarkerOld,
        title: POIArrayVisited[i][2]
    });

    google.maps.event.addListener(markerVisited, "click", function() {
            //link and update cookie
            document.cookie = "OpenPOI" + "=" + OpenPOI;
            window.location.href = "poi.php";
        });

}

In your code:

// 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";
    });

}

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 the POIArrayVisited but you are not obtaining a new instance of the markerVisited. So that's probably the reason only the LAST marker actually responds on the click.

Should be something similar to:

for (var i = 0; i < POIArrayVisited.length; i++) {
    var markerLatlng = new google.maps.LatLng(POIArrayVisited[i][0], POIArrayVisited[i][1]);
    // Place a hit marker
    var markerVisited = new google.maps.Marker({
        position: markerLatlng,
        map: map,
        icon: imageMarkerOld,
        title: POIArrayVisited[i][2]
    });

    google.maps.event.addListener(markerVisited, "click", function() {
            //link and update cookie
            document.cookie = "OpenPOI" + "=" + OpenPOI;
            window.location.href = "poi.php";
        });

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文