Google Maps API v3: Opening the link on a marker in a new window

seems like a pretty basic thing, but I don't seem to be finding the answer. Here's my Google Maps code:

var marker = new google.maps.Marker({
      position: new google.maps.LatLng(123, 123),
      url: "http://www.google.com",
      map: map
    });

...

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

What do I need to change, to open the link in a new window?

Many thanks!

Answers:

Answer

You have to use the method open of the window object like this:

window.open("http://www.w3schools.com");

So your code will be something like this:

google.maps.event.addListener(marker, 'click', function() {
    window.open(marker.url);
});

Visit this link for an overview of the open method and the options you can set.

Answer

So I used this approach but when I was in a loop making lots of markers, each with a different URL, this was the approach I needed to take.

    var newLatlng = new google.maps.LatLng(row[0], row[1]);
    var marker = new google.maps.Marker({
        position: newLatlng,
        map: map,
        title: row[2],
        url: row[3]
    });
    google.maps.event.addListener(marker, 'click', function() {
        window.open('https://www.youtube.com/watch?v='+this.url);
    });

Note that the key was using this.url in the listener code. If I used marker.url in the listener code, it would open the last of many urls for all of the markers.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.