DEV Community

Cover image for How to Add Markers to Google Maps in React.js
Aneeqa Khan
Aneeqa Khan

Posted on • Originally published at aneeqa-k25.Medium

How to Add Markers to Google Maps in React.js

Hello Everyone!

In the previous article, I shared the steps to integrate Google Maps into your React.js Application.

Today, I want to show how to add markers to the map. So, I aim to implement a click on the specific location on the map and save it in a list.

Create New States

First of all, I want to create some new states to show/hide the dialog box, store the dialog box location, and store the clicked location.

// store clicked location
const [selectedLocation, setSelectedLocation] = useState({});
// store show dialog state to add location
const [showDialog, setShowDialog] = useState(false);
// store dialog location
const [dialogLocation, setDialogLocation] = useState("");
Enter fullscreen mode Exit fullscreen mode

Create handleMapClick Function

I created the handleMapClick function to show a dialog box containing a button to add this location to the list.

  // handle click on map
  const handleMapClick = (mapProps) => {
    // checks if location clicked is valid
    if (mapProps.detail.placeId) {
      const lat =;
      const lng = mapProps.detail.latLng.lng;
      setDialogLocation({ lat, lng });
      setSelectedLocation({ lat, lng });
    } else {
      // show alert message
      alert("Please select the specific location");
Enter fullscreen mode Exit fullscreen mode

Here, I am checking the placeId of a location, which means it is a location with a name and ID. Otherwise, it will show an alert to select a specific location.

Calling this function in the <Map/> component.

  style={{ borderRadius: "20px" }}
  onClick={(mapProps) => handleMapClick(mapProps)}
Enter fullscreen mode Exit fullscreen mode

Once the correct location is clicked, I want to show the button to confirm from the user to add this location.

Show Add Location Button

Here is the code to show the button in the dialog box on the clicked location of the map.

import { InfoWindow } from "";

{showDialog && (
  // displays a dialog to add clicked location
  <InfoWindow position={dialogLocation}>
    <button className="app-button" onClick={onAddLocation}>
      Add this location
 <Marker position={markerLocation} />
Enter fullscreen mode Exit fullscreen mode

It will show the dialog like this

google map with marker and info box

Here on the "Add this location" button, I passed the "onAddLocation" function to it.

Save Clicked Location in the List

For this, we need a new state to store all the clicked locations.

// store list of all locations selected
const [listOfLocations, setListOfLocations] = useState([]);

  // add location to show in a list
  const onAddLocation = () => {
    // Create a Google Maps Geocoder instance
    const geocoder = new;

    // Reverse geocode the coordinates to get the place name
    geocoder.geocode({ location: selectedLocation }, (results, status) => {
      if (status === "OK") {
        if (results[0]) {
            { name: results[0].formatted_address, location: selectedLocation },
      } else {
        console.error("Geocoder failed due to: " + status);
Enter fullscreen mode Exit fullscreen mode

Here I am reverse geocoding the coordinates to get the location name and saving the name and LAT and LNG with it.

Display List of Locations

After successfully storing the location in the list, I want to display the locations with the “View” and “Delete” functions.

<div className="list-container">
  {/* checks the location list is not empty */}
  {listOfLocations.length > 0 ? (
    <p className="list-heading">List of Selected Locations</p>
    {/* displays stored locations  */}
    { => {
      return (
          key={ + loc.location.lng}
          <p className="latLng-text">{}</p>
          <div style={{ display: "flex" }}>
            <AppButton handleClick={() => onViewLocation(loc.location)}>
              handleClick={() => onDeleteLocation(loc.location)}
   ) : (
     // displays text msg to select a location
       <p className="list-heading">
         Select a location from map to show in a list
Enter fullscreen mode Exit fullscreen mode

Now on the View button click, I’ll show the marker to the specific location and the Delete button click will delete the location from the list.

  // displays marker on the map for the selected location
  const onViewLocation = (loc) => {
    setMarkerLocation({ lat:, lng: loc.lng });

  // deletes selected location from the list
  const onDeleteLocation = (loc) => {
    let updatedList = listOfLocations.filter(
      (l) => !== && loc.lng !== l.location.lng
Enter fullscreen mode Exit fullscreen mode

Here is the screenshot after all the work.

google map with selected lists

That’s it for today.
You can check the whole code on my github.

Thank you for reading! Feel free to connect with me on LinkedIn or GitHub.

Top comments (0)