🥝GuideKiwi
Free Guide

Free Guide to Google Maps API Multiple Pins

Understanding Google Maps API and Multiple Pins Basics Google Maps API is a set of tools that developers use to add maps to websites and applications. When y...

GuideKiwi Editorial Team·

Understanding Google Maps API and Multiple Pins Basics

Google Maps API is a set of tools that developers use to add maps to websites and applications. When you embed a Google Map on a website, you're using the Maps JavaScript API, which is the most common version. The API allows you to customize how maps appear and function on web pages.

A "pin" in mapping terminology refers to a marker—a visual indicator placed on a map at specific coordinates. These markers show locations of interest, such as business addresses, event venues, or points along a route. When you work with multiple pins, you're displaying several locations simultaneously on one map.

Google Maps API is genuinely free to use within certain limits. Google provides a free tier that includes 28,000 map loads per month for the Maps JavaScript API. This means most small to medium websites can display maps without paying anything. Additional requests beyond the free tier are charged at rates starting around $7 per 1,000 requests, though you receive a monthly credit of $200 that covers many use cases.

Before you can use Google Maps API, you need to set up a Google Cloud project and obtain an API key. This key acts as a credential that tells Google's servers who is making the request. Without a valid API key, your map won't display. The setup process involves creating a free Google Cloud account, enabling the Maps JavaScript API service, and generating a key specifically for your project.

Multiple pins serve practical purposes on websites. A restaurant directory uses pins to show all location addresses. Real estate websites display property listings as pins on neighborhood maps. Event management sites mark multiple venue locations. Delivery services show store locations customers can visit. Each pin can contain information accessed by clicking or hovering over it.

Practical Takeaway: Google Maps API provides a no-cost starting point for displaying location data. Understanding the difference between the API itself, markers (pins), and the free tier limitations helps you plan whether this solution fits your needs.

Setting Up Your Google Cloud Project and API Key

Creating your Google Cloud project is the foundation for using Google Maps API. Visit the Google Cloud Console at console.cloud.google.com. If you don't have a Google account, you'll need to create one first. This account will manage all your API projects and billing settings.

Once logged into the Google Cloud Console, create a new project by clicking the project dropdown at the top of the page. Select "New Project" and give it a descriptive name like "Restaurant Map" or "Store Locator." Google assigns each project a unique project ID automatically. This ID identifies your project within Google's systems. After naming your project, click "Create" and wait a few moments for Google to set it up.

Next, you'll enable the Maps JavaScript API for your project. In the Google Cloud Console, navigate to the "APIs & Services" section. Click "Enable APIs and Services" and search for "Maps JavaScript API." Click on it and press the "Enable" button. This tells Google you want to use this specific API service. You may see other map-related APIs like Directions API or Places API—for basic multiple pins, you only need the JavaScript API enabled.

Creating your API key comes after enabling the API. Return to "APIs & Services" and click "Credentials" in the left menu. Click "Create Credentials" and select "API Key." Google generates a long string of characters—this is your API key. You'll see options to restrict your key's use. Setting restrictions is optional but recommended for security. You can restrict the key to specific websites or specific APIs.

Protecting your API key prevents unauthorized use. Anyone with your key could theoretically use your quota and incur charges. Best practice involves restricting your key to specific domains where you'll use it. For example, if your website is example.com, you restrict the key to only work on example.com. This prevents someone from copying your key and using it on their own site.

Store your API key somewhere safe in your code. When you write the HTML to display your map, you'll include your API key in the script tag that loads Google Maps. Many developers store their key in environment variables or configuration files rather than hardcoding it directly into their public source code.

Practical Takeaway: Set up your Google Cloud project first, enable the Maps JavaScript API, and generate a restricted API key. This process takes about 10-15 minutes and provides the credentials you need to display maps on your website.

Creating the HTML Structure for Your Map Container

The HTML structure provides the foundation where your map displays. At its simplest, you need a container—an HTML element where the map will render. This is typically a div element with an id attribute that you'll reference in your JavaScript code.

Here's a basic HTML structure:

  • Create a div element with an id like "map"
  • Add CSS styling to set the map's width and height
  • Include a script tag that loads the Google Maps library with your API key
  • Write JavaScript code that initializes the map and adds pins

The container div must have explicit width and height values. If you don't set these, the map won't display visibly on your page. Common practice involves setting the container to fill most of the screen—for example, 100% width and 400-600 pixels in height. You can use CSS to style it however you prefer.

Your HTML file needs several key elements working together. First, the container div gives the map a place to live on the page. Second, the CSS styling makes that container visible with defined dimensions. Third, the script tag that loads Google Maps must include your API key as a parameter. Fourth, your custom JavaScript code initializes the map and adds pins.

A practical example structure includes a simple page with a header, the map container, and any information you want to display alongside it. Many websites put the map on the right side and a list of locations on the left. Others make the map take up the full page width. The layout depends on your site's design and purpose.

The script tag that loads Google Maps should appear near the end of your HTML body section. This tag references Google's hosted JavaScript library and includes your API key. The order matters—your custom JavaScript code that initializes the map must come after the Google Maps library loads, or you'll get errors.

Mobile responsiveness matters for map containers. Since many people view websites on phones, your map container should resize appropriately on smaller screens. CSS media queries can adjust the map's height on mobile devices. A map that's 500 pixels tall on desktop might only be 300 pixels on mobile to avoid taking over the entire screen.

Practical Takeaway: Build a simple HTML structure with a clearly defined container div, set explicit width and height values, and load the Google Maps library with your API key in the correct order. This setup provides the skeleton your map JavaScript will populate.

Writing JavaScript Code to Initialize Your Map and Add Multiple Pins

JavaScript code brings your map to life and places the pins on it. The Google Maps library provides a Map object that you initialize with specific settings. You tell Google Maps where to center the map, how zoomed in it should be, and other display options.

The initialization code creates a new map instance. You reference your container div by its id, then pass an options object that specifies the map's center coordinates (latitude and longitude), zoom level, and map type. For example, zoom level 12 shows a city neighborhood, while zoom level 5 shows an entire state. The center coordinates tell Google Maps where to display the map initially.

Adding markers (pins) to your map involves creating Marker objects. Each marker needs at least three pieces of information: latitude, longitude, and the map it belongs to. Optionally, you can add a title (tooltip that appears on hover), a label (letter or number shown on the pin), and custom information that appears in an info window when the user clicks the marker.

A simple JavaScript example creates an array of location objects. Each location contains name, latitude, longitude, and address information. Your code loops through this array and creates a marker for each location. This approach scales well—adding more pins just means adding more objects to your array, and the loop creates markers for all of them automatically.

Info windows display additional information when users interact with markers. When someone clicks a marker, an info window pops up showing details about that location. The content can be plain text or formatted HTML. You can customize the appearance—changing

🥝

More guides on the way

Browse our full collection of free guides on topics that matter.

Browse All Guides →