ArticleZip > Adding Marker To Google Maps In Google Map React

Adding Marker To Google Maps In Google Map React

Google Map React is a popular library for integrating Google Maps into web applications with ease. One common task developers often need to accomplish is adding markers to their maps to pinpoint specific locations. In this article, we will walk you through the process of adding markers to Google Maps using Google Map React.

To get started, you first need to install the Google Map React library in your project. You can do this using npm or yarn by running the following command in your terminal:

Bash

npm install google-map-react

Once you have the library installed, you can import it into your codebase. To add a Google Map with a marker to your project, you can follow these simple steps:

1. Import the necessary components from Google Map React in your component file:

Javascript

import GoogleMapReact from 'google-map-react';

2. Create a functional component that will render the Google Map and the marker:

Javascript

const MapWithMarker = () => {
  const mapProps = {
    center: { lat: 37.7749, lng: -122.4194 },
    zoom: 12,
  };

  return (
    <div style="{{">
      
        
      
    </div>
  );
};

3. Define the `Marker` component:

Javascript

const Marker = ({ text }) =&gt; (
  <div style="{{">{text}</div>
);

In the example above, we create a new component called `MapWithMarker` that renders a Google Map centered at coordinates `{ lat: 37.7749, lng: -122.4194 }` with an initial zoom level of 12. We also add a marker at the center with the text "My Marker" using the `Marker` component.

Make sure to replace `'YOUR_GOOGLE_MAPS_API_KEY'` with your actual Google Maps API key, which is required for using the Google Maps services.

By following these steps, you can easily add a marker to a Google Map using Google Map React in your web application. Feel free to customize the marker's appearance and behavior based on your project requirements.

That's it! You should now have a fully functional Google Map with a marker added to your web application. Happy mapping!

×