Avatar of sonic1234
sonic1234
Flag for Australia asked on

Add Clickable Links to Google Map Markers

Hello Experts,

I want when users click on my markers that they go to a URL in a new window.  My code so far - I am missing something here;

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="robots" content="noindex, nofollow">
    <meta name="googlebot" content="noindex, nofollow">



    <style type="text/css">
        #map_canvas {

            height: 400px;
            width: 100%;

        }
    </style>
    </title>





    <script type='text/javascript'>
        //<![CDATA[
        window.onload = function() {
                var locations = [
                    ['Bondi Beach', -33.890542, 151.274856, 4, 'http://www.google.com'],
                    ['Coogee Beach', -33.923036, 151.259052, 5, 'http://microsoft.com'],
                    ['Cronulla Beach', -34.028249, 151.157507, 3, 'http://www.apple.com'],
                    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2, 'http://www.facebook.com'],
                    ['Maroubra Beach', -33.950198, 151.259302, 1, 'http://www.twitter.com']
                ];
                var map;
                var markers = [];

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

                    var num_markers = locations.length;
                    for (var i = 0; i < num_markers; i++) {
                        markers[i] = new google.maps.Marker({
                            position: {
                                lat: locations[i][1],
                                lng: locations[i][2]
                            },
                            map: map,
                            html: locations[i][0],
                            url: locations[i][4],
                            id: i,
                        });

                        google.maps.event.addListener(markers[i], 'click', function() {
                            window.location.href = marker.url;
                        });
                    }
                }

                init();
            } //]]>
    </script>


</head>

<body>
    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>
    <div id="map_canvas"></div>

</body>

</html>

Open in new window

JavaScript

Avatar of undefined
Last Comment
Kim Walker

8/22/2022 - Mon
Kim Walker

window.location.href changes the url of the current window. window.open should open the url in a new window. (See line 59 of your code.)
window.open = marker.url;

Open in new window

sonic1234

ASKER
Thank you, there must be some other error with my code because that change still does not result in the marker clicks working

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="robots" content="noindex, nofollow">
    <meta name="googlebot" content="noindex, nofollow">



    <style type="text/css">
        #map_canvas {

            height: 400px;
            width: 100%;

        }
    </style>
    </title>





    <script type='text/javascript'>
        //<![CDATA[
        window.onload = function() {
                var locations = [
                    ['Bondi Beach', -33.890542, 151.274856, 4, 'http://www.google.com'],
                    ['Coogee Beach', -33.923036, 151.259052, 5, 'http://microsoft.com'],
                    ['Cronulla Beach', -34.028249, 151.157507, 3, 'http://www.apple.com'],
                    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2, 'http://www.facebook.com'],
                    ['Maroubra Beach', -33.950198, 151.259302, 1, 'http://www.twitter.com']
                ];
                var map;
                var markers = [];

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

                    var num_markers = locations.length;
                    for (var i = 0; i < num_markers; i++) {
                        markers[i] = new google.maps.Marker({
                            position: {
                                lat: locations[i][1],
                                lng: locations[i][2]
                            },
                            map: map,
                            html: locations[i][0],
                            url: locations[i][4],
                            id: i,
                        });

                        google.maps.event.addListener(markers[i], 'click', function() {
                            window.open = marker.url;
                        });
                    }
                }

                init();
            } //]]>
    </script>


</head>

<body>
    <script src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>
    <div id="map_canvas"></div>

</body>

</html>

Open in new window

ASKER CERTIFIED SOLUTION
Kim Walker

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck