React Google Map With A Custom Pin Marker

Most of the application uses google maps in the application to track down live location or data. As we have many frameworks in the market implementing maps in all those frameworks has a different procedure. And customizing google map and matching it to our application theme is really a challenge, So in this article, we are going to see how to implement React google maps custom marker in React also with the customization of the marker.

Prerequisites

This article assumes that you have practical knowledge about React JS and google API’s. And working with npm libraries.

Preface

This article is going to be covering how to set-up and implement google maps React custom marker and also how to customize the marker pin.

We will follow the steps below in order:-

  1. Getting Google API key of google map.
  2. Installation of Google map Library to react.
  3. Implementation of the map.
  4. Customization of markers.

Now let’s start the integration process.

Check On-Demand Logistic Case Study

Get Google Map API Key

  1. First login to your google account
  2. Go to the Google Cloud Platform Console.
  3. Now click on create a new project, and create a new project.

4. Now click on create credentials and save it.

5. Now just copy your API key and use it in your React Project.

Also Read: Branch Deep Linking in ReactJs

Starting With The Development Process

1. Installation

npm i React-google-maps

We’ve installed a third-party library react-google-map in our project. Now we are good at implementing maps.

2. Creating Component For Google Map

So first we are going to build a map component. Its purpose is to render the data inside of the google map component, which comes from the package that we installed. It does not require any extra props, we just need to pass the zoom level of the map and where to center the map that is pretty typical.

import React, { Component } from 'React'

import { Map, GoogleApiWrapper } from 'google-maps-React';

export class MapComponent extends Component {

    render() {

        return (<div className="map-area">

                <Map

                    google={this.props.google}

                    zoom={14}

                    center={{

                        lat: 47.444,

                        lng: -122.176

                    }}

                ></Map>

            </div>);

    }

}

export default GoogleApiWrapper({

    apiKey: (‘your_api_key’)

})(MapComponent);

3. Adding Initial Marker

Now, we are moving to show the marker on the map cause we are using the map in the application definitely to show some location for that we are using the “Marker” class provided by the library that we installed. We need to pass position props to marker it including the latitude and longitude.

E.g:- {lat: 47.444, lng: -122.176}

import React, { Component } from 'React'

import { Map, GoogleApiWrapper, Marker } from 'google-maps-React';

export class MapComponent extends Component {

    render() {

        return (<div className="map-area">

            <Map

                google={this.props.google}

                zoom={14}

                center={{

                    lat: 47.444,

                    lng: -122.176

                }}>

                <Marker key="marker_1"

                    position={{

                        lat: 47.444,

                        lng: -122.176

                    }}

                />

            </Map>

        </div>);

    }

}

export default GoogleApiWrapper({

    apiKey: (‘your_api_key’)

})(MapComponent);

4. Customize Marker

Now, after adding the marker in the map it looks like the red default pin that we see in google map. So to match it with the application theme we need to customize it, so while customizing it we need to take care of two things:

  1. Size the marker pin
  2. It should point to the right location on the map
<Marker key="marker_1"

    icon={{

        url: 'https://cdn.mindbowser.com/custom_marker_pin.svg',

        anchor: new google.maps.Point(17, 46),

        scaledSize: new google.maps.Size(37, 37)

    }}

    position={{

        lat: 47.444,

        lng: -122.176

    }}

/>

So we need to pass icon props to the marker. It changes the style of the marker, It replaces the default marker with the image that we are providing in the URL. I recommend using SVG images for Marker pins. But it’s not that easy to pass icon prop and get the new pin on map, we need to manage it to locate it in the right location. Because when we change the image it also changes the size of the marker so using an anchor and scaled size props we can manage it.

Do you want to Hire ReactJs Expert?

Points To Remember

  1. You must need a paid API key to work with the map, if you don’t have a paid key then it renders ‘map is under development’ all over the map.
  2. When you’re working with a map, then the browser needs to access location service so for that we need secure connection HTTPS.
coma

Conclusion

From the above article, we covered how to integrate the react-google-map library into our react application and also cover how to customize in the marker pin and set it to an accurate location. Write your quaries in the coment section if you have any doubt about how to add google map in React js.

Prasad

Tech Expert

Prasad is a Full-stack developer with around 3+ years of experience. He is an expert in building Python integrated web applications, Creating REST APIs with well designed, testable, and efficient, and optimized code. He is having experience in web technologies like ReactJS, JavaScript, etc. He loves to solve technical problems, learn new technologies, and helping others.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks