We help IT Professionals succeed at work.
Get Started

Add Clickable Links to Google Map Markers

sonic1234
sonic1234 asked
on
3,711 Views
Last Modified: 2018-02-06
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

Comment
Watch Question
Web Programmer/Technician
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE