简体   繁体   English

添加指向 Google 地图标记的可点击链接

[英]Adding a clickable link to a Google Map marker

I have a google map that pulls info from a database and populates it with markers, name, address, etc. In the same database is a URL called "slug".我有一个谷歌地图,它从数据库中提取信息并用标记、名称、地址等填充它。在同一个数据库中有一个名为“slug”的 URL。 I'd like to make the Name/Address popup for each marker be a clickable link taking you to "slug".我想让每个标记的名称/地址弹出窗口成为一个可点击的链接,将您带到“slug”。

Here's the map so far...这是目前为止的地图...

And here's the code so far:这是到目前为止的代码:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Google Maps AJAX + mySQL/PHP Example</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    <script type="text/javascript">
        //<![CDATA[

        var customIcons = {
            restaurant: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
                shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            bar: {
                icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
                shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
        };

        function load() {
            var map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(47.6145, -122.3418),
                zoom: 5,
                mapTypeId: 'roadmap'
            });

            var infoWindow = new google.maps.InfoWindow;

            // Change this depending on the name of your PHP file
            downloadUrl("phpsqlsearch_genxml.php", function(data) {
                var xml = data.responseXML;
                var markers = xml.documentElement.getElementsByTagName("marker");
                for (var i = 0; i < markers.length; i++) {
                    var first = markers[i].getAttribute("first");
                    var last = markers[i].getAttribute("last");
                    var line_1 = markers[i].getAttribute("line_1");
                    var line_2 = markers[i].getAttribute("line_2");
                    var line_3 = markers[i].getAttribute("line_3");
                    var city = markers[i].getAttribute("city");
                    var state = markers[i].getAttribute("state");
                    var zipcode = markers[i].getAttribute("zipcode");
                    var address = markers[i].getAttribute("address");
                    var type = markers[i].getAttribute("type");
                    var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lng")));
                    var html = "<b>" + first + "\xa0" + last + "</b> <br/>" + line_1 + "<br/>" + (line_2 ? line_2 + "<br/>" : "") + (line_3 ? line_3 + "<br/>" : "") + city + "\xa0" + "," + state + "\xa0" + zipcode;
                    console.log(html);
                    var icon = customIcons[type] || {};
                    var marker = new google.maps.Marker({
                        map: map,
                        position: point,
                        icon: icon.icon,
                        shadow: icon.shadow
                    });
                    bindInfoWindow(marker, map, infoWindow, html);
                }
            });
        }

        function bindInfoWindow(marker, map, infoWindow, html) {
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(html);
                infoWindow.open(map, marker);
            });
        }

        function downloadUrl(url, callback) {
            var request = window.ActiveXObject ?
                new ActiveXObject('Microsoft.XMLHTTP') :
                new XMLHttpRequest;

            request.onreadystatechange = function() {
                if (request.readyState == 4) {
                    request.onreadystatechange = doNothing;
                    callback(request, request.status);
                }
            };

            request.open('GET', url, true);
            request.send(null);
        }

        function doNothing() {}

        //]]>
    </script>
</head>

<body onload="load()">
    <div id="map" style="width: 100%; height: 100%"></div>
</body>

</html>

After:后:

var html = "<b>" + first + "\xa0" + last + "</b> <br/>" + line_1 + "<br/>" + (line_2 ? line_2 + "<br/>" : "") + (line_3 ? line_3 + "<br/>" : "") + city + "\xa0" + "," + state + "\xa0" + zipcode;

add:添加:

var slug = markers[i].getAttribute("slug");
html = "<a href='" + slug + "'>" + html + "</a>";

This will make all the text inside the popup a clickable link这将使弹出窗口中的所有文本成为可点击的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM