Google Maps Js Api V3 – Simple Multiple Marker Example
Google Maps JS API v3 – Simple Multiple Marker Example
Google Maps is a powerful tool that allows developers to integrate interactive maps into their web applications. In this blog post, we will explore how to use the Google Maps JavaScript API v3 to display multiple markers on a map.
Step 1: Set up the HTML
First, let’s set up the HTML structure for our map. We will create a
element with an id of “map” to hold the map.
Step 2: Include the Google Maps API
To use the Google Maps JavaScript API, we need to include the API script in our HTML. Add the following script tag just before the closing
tag:
Replace YOUR_API_KEY with your own Google Maps API key. If you don’t have an API key, you can obtain one by following the instructions on the Google Maps API documentation.
Step 3: Initialize the Map
Now, let’s initialize the map using the Google Maps JavaScript API. Add the following JavaScript code inside a tag:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.7749, lng: -122.4194},
zoom: 10
});
}
This code creates a new map object and sets the initial center and zoom level. The center is set to San Francisco, and the zoom level is set to 10.
Step 4: Add Markers
Finally, let's add multiple markers to the map. Add the following JavaScript code inside the initMap function:
This code creates an array of marker objects, each with a latitude and longitude. It then loops through the array and creates a new marker for each set of coordinates, adding it to the map.
Step 5: Putting It All Together
Here's the complete code:
When you run this code, you should see a map with three markers at the specified locations. Feel free to modify the coordinates and add more markers as needed.
That's it! You now have a simple multiple marker example using the Google Maps JavaScript API v3. Happy mapping!
Leave a Reply