Home >Web Front-end >JS Tutorial >How to Display Multiple Markers with InfoWindows using the Google Maps JS API v3?

How to Display Multiple Markers with InfoWindows using the Google Maps JS API v3?

Barbara Streisand
Barbara StreisandOriginal
2024-12-20 15:45:10740browse

How to Display Multiple Markers with InfoWindows using the Google Maps JS API v3?

Google Maps JS API v3 - Simple Multiple Marker Example

Creating a Map and Markers

To begin, create a new HTML file and include the Google Maps API script with your API key. Next, set up the Map object and provide it with the necessary parameters:

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

Creating an InfoWindow

Define an InfoWindow object, which displays the location name upon marker click:

var infowindow = new google.maps.InfoWindow();

Looping Through Location Data

Next, iterate through your data array and create a marker for each location, adding it to the map:

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

Setting Click Events for InfoWindows

Finally, add an event listener to each marker that triggers the InfoWindow to open when clicked:

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

Complete Code

The complete code looks like this:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

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

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

The above is the detailed content of How to Display Multiple Markers with InfoWindows using the Google Maps JS API v3?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn