How to Use Google Maps Platform’s Extended Component Library

Jun 10, 2022 | Programming

If you are looking to enhance your web applications with powerful mapping capabilities, the Google Maps Platform’s Extended Component Library is your perfect ally. This toolkit allows developers to construct intricate map interfaces quickly and with minimal effort. Let’s dive into how to install and utilize this extended library to create stunning maps!

Installation

To get started with the Extended Component Library, you need to load the library into your project.

Loading the Library

  • For Applications that Bundle Their Code
    For optimal performance, use a package manager and import only the components you need. To install, you can run:
  • npm i @googlemapsextended-component-library

    Once installed, import the components in your application like this:

    import '@googlemapsextended-component-library/overlay_layout.js';
  • To Load All Components
    Alternatively, you can include a CDN-hosted bundle directly in your HTML file as a module script:
  • <script type="module" src="https://unpkg.com/@googlemapsextended-component-library"></script>

Getting Your API Key

The components use Google Maps Platform APIs. Begin by signing up for Google Maps Platform and creating an API key. Place the API Loader element in your HTML to specify your API key:

<gmpx-api-loader key="YOUR_API_KEY"></gmpx-api-loader>

By default, all APIs will be enabled, but you can opt for only the necessary ones as indicated in the component’s documentation.

Usage

This library contains a wealth of Web Components that are usable anywhere in the HTML of your web app, and they work seamlessly in all modern browsers.

Displaying Information About a Place

Here is a simple example showcasing how to display information about a specific place:

<!-- Load the library -->
<script type="module" src="https://unpkg.com/@googlemapsextended-component-library"></script>
<!-- Configure and load the Maps JS SDK with your API key -->
<gmpx-api-loader key="YOUR_API_KEY"></gmpx-api-loader>
<gmpx-split-layout>
    <gmpx-place-overview slot="fixed" place="ChIJ39Y-tdg1fYcRQcZcBb499d"></gmpx-place-overview>
    <gmp-map slot="main" center="43.880,-103.459" zoom="10" map-id="DEMO_MAP_ID">
        <gmp-advanced-marker position="43.880,-103.459"></gmp-advanced-marker>
    </gmp-map>
</gmpx-split-layout>

Panning to Marker with JavaScript

Adding interactivity with JavaScript can elevate user experience. Here’s how to add a pan-to-marker feature:

<!-- Load the library -->
<script type="module" src="https://unpkg.com/@googlemapsextended-component-library"></script>
<!-- Configure and load the Maps JS SDK with your API key -->
<gmpx-api-loader key="YOUR_API_KEY"></gmpx-api-loader>
<gmp-map id="my-map" center="33.15,-96.20" zoom="10" map-id="DEMO_MAP_ID">
    <gmp-advanced-marker class="pannable" gmp-clickable position="33.15,-96.20"></gmp-advanced-marker>
</gmp-map>
<script>
    window.addEventListener('load', () => {
        const mapElement = document.getElementById('my-map');
        const markers = document.querySelectorAll('gmp-advanced-marker.pannable');
        markers.forEach((marker) => {
            marker.addEventListener('gmp-click', () => {
                mapElement.innerMap.panTo(marker.position);
            });
        });
    });
</script>

Think of the components as building blocks in a map architecture. You can easily create combinations of these building blocks to form a robust map application while simplifying the intricate details into a single HTML element.

Framework Support

The Extended Component Library harmonizes beautiful maps with popular frontend frameworks such as Angular, React, or Vue.js. For seamless integration, refer to your framework’s documentation. Below are a couple of specific examples:

  • React: Enjoy native React components for an effortless experience. Check out the React Support
  • Angular: Angular also pairs nicely with Web Components. Explore the Angular example app.

Styling and Theming

Using CSS custom properties, you can style components to match your design preferences. For instance, if you wanted to change the background color of the Place Picker, you can use:

body {
    --gmpx-color-surface: blue;
}

Troubleshooting

If you encounter issues while using the Extended Component Library, here are some common troubleshooting tips:

  • Ensure your API key is correctly placed in your HTML.
  • Check if the required APIs are enabled in your Google Cloud Console.
  • Confirm the CDN link is correctly inserted in your HTML file.
  • If components are not rendering, inspect your browser console for any error messages.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With the Extended Component Library, your web applications can come alive with dynamic and interactive maps effortlessly. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox