Home > Web Front-end > JS Tutorial > How to Easily Add Multiple Markers with Custom InfoWindows to a Google Map using the Google Maps JS API v3?

How to Easily Add Multiple Markers with Custom InfoWindows to a Google Map using the Google Maps JS API v3?

Susan Sarandon
Release: 2024-12-18 17:00:11
Original
145 people have browsed it

How to Easily Add Multiple Markers with Custom InfoWindows to a Google Map using the Google Maps JS API v3?

Google Maps JS API v3 - A Straightforward Example for Multiple Markers

Creating multiple markers on a Google Map can be a daunting task, especially for newcomers to the API. To simplify this process, we will delve into a straightforward example that will guide you through the necessary steps.

Consider the following data array provided by Google:

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]
];
Copy after login

The goal is to plot these markers on a map and display an infoWindow containing the name when a marker is clicked.

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?key=YOUR_API_KEY" 
          type="text/javascript"></script>
</head> 
<body>
  <div>
Copy after login

This code creates a map centered around Sydney, Australia, with a zoom level of 10. It dynamically generates markers for each beach in the array, and each marker displays an infoWindow with the beach's name on click.

Note that the event listener for the click event uses a closure to preserve the values of the marker and array index during the execution of the callback function.

By following these steps, you can easily add multiple markers with custom infoWindows to your own Google Maps visualizations.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template