Home ยป React google maps draggable marker example

React google maps draggable marker example

react google maps draggable marker example; In this tutorial, we will learn how to integrate Google Maps with draggable markers into the react application, We will use the google-map-react npm package, this component is written in Reactjs using the google map API, it allows you to render a map on your website, and it allows customization in google maps like adding markers, zooming out, zooming in, info window, info window, etc… If you want to implement a simple Google Maps in react, please read here.

You can download the working example https://github.com/technostuf/react-google-map-markers-draggable

Create a React application

Create a React app using the below command

  create-react-app react-google-map-markers-draggable

After running the above command system will create a new folder “react-google-map-markers-draggable” so you need to move into that folder, use the below command to change the directory.

 cd react-google-map-markers-draggable

Install google-map-react library

 npm install --save google-map-react
 OR using yarm

 yarn add google-map-react

Add code to the file

Create a GoogleMapComponent.js component file to hold google map code separate from another component and put the below code into that file

import React, { useState } from "react";
import { Map, GoogleApiWrapper, Marker } from 'google-maps-react';
const GoogleMapComponent = (props) => {

    let markersList = [
        { lat: 23.024000, lng: 72.580276 },
        { lat: 23.0063, lng: 72.6026 }
    let [markers, setMarkers] = useState(markersList);
    const mapStyles = {
        width: '100%',
        height: '100%'

    let onMarkerDragEnd = (coord, index, markers) => {
        const { latLng } = coord;
        const lat = latLng.lat();
        const lng = latLng.lng();
        markers[index] = { lat, lng };

    let myMarkers = markers && Object.entries(markers).map(([key, val]) => (
        <Marker key={key} id={key} position={{
            lat: val.lat,
            lng: val.lng
            onClick={() => console.log("Clicked")}
            onDragend={(t, map, coord) => onMarkerDragEnd(coord, key, markers)}
    return (
                <div className="row d-flex justify-content-center text-center">
                    <h1>React Google Recaptcha V3 example - technostuf.com</h1>
                                lat: 23.033863,
                                lng: 72.585022
export default GoogleApiWrapper({
    apiKey: 'API KEY'

After adding a code in GoogleMapComponent, you have called this component file into your code, in my case I am adding it to App.js.

Make sure you have to change the API key to your Google Map API key, please follow the article to get Google Map API Key https://developers.google.com/maps/documentation/javascript/get-api-key

import logo from './logo.svg';
import './App.css';
import GoogleMapComponent from './GoogleMapComponent';

function App() {
  return (
    <div className="App">
      <GoogleMapComponent />
export default App;

Run a React application

Run the React application using the following command.

 npm start

After compilation, the program opens your browser and runs http://localhost:3000/


react google maps draggable marker

You can download the working example of react google maps draggable marker


Related Post

Leave a Comment

Your email address will not be published.

2 × four =