DEV Community

Cover image for Google Maps API Directions Service in React – Plot efficient route on map with total distance and duration.
Anlisha Maharjan
Anlisha Maharjan

Posted on • Originally published at anlisha.com.np on

Google Maps API Directions Service in React – Plot efficient route on map with total distance and duration.

Use Case:

  • One can enter source and destination address; For the address field, google places autocomplete is implemented.
  • One can also add multiple way-points/stops between source and destination address.
  • On change in any address field, The Map and the Directions service is initialized to plot the driving directions and route on map and display the total distance and total duration.

Final Demo!

Step1 — Enable Directions API for Google Maps API key

If you are new to Google Maps API key, I recommend you to have a look at it here.

Step2 — Start Basic CRA and Install packages

npx create-react-app axon
cd axon
npm install @mui/material @emotion/react @emotion/styled
Enter fullscreen mode Exit fullscreen mode

This will create a basic CRA. We also installed MUI 5 for creating UI components.

npm install @react-google-maps/api use-places-autocomplete formik moment
Enter fullscreen mode Exit fullscreen mode

This will install react-google-maps/api and use-places-autocomplete packages required.

Step3 — Include Maps JavaScript Library

Include the google maps client-side library in public/index.html

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key [YOUR_API_KEY]&libraries=places"></script>
Enter fullscreen mode Exit fullscreen mode

Step4 — Setup App.js

Initialize Formik with empty initial values of address fields. Formik is the most popular open source form library for React.

Step5 — Create source and destination address field with multiple way-points

The GoogleAutocomplete field used is a custom google places autocomplete. You can find it here.

Step6 — Configure Request to Direction Service

On line 9, an instance of the DirectionsService Object is created.

On line 13, the route() called takes directions request JavaScript object (with required query parameters origin, destination and travelMode) as an argument. Also waypoints parameter is included to consider all the stops between origin and destination.

The second argument of route() method which is a response callback function returns directionsResult and directionsStatus.

And that concludes it!

Source Code!

The full source code is available here — https://github.com/anlisha-maharjan/react-google-directions-service

Happy Learning! Feel free to give this article a clap and follow to stay up to date with more articles!

The post Google Maps API Directions Service in React – Plot efficient route on map with total distance and duration. first appeared on Anlisha Maharjan.

Oldest comments (0)

Top Posts from the React Ecosystem

1. Changes In The Official React Documentation

The former React Docs Beta has been officially released as the updated React documentation at react.dev after years of hard work and refinement. Check out the brand new React Docs: What’s New in the Updated React Docs

2. CRA's Time is Over

React developer team has removed create-react-app (CRA) from official documentation rendering it no longer the default setup method for new projects. The bulky setup, slow, and outdated nature of CRA led to its removal: create-react-app is officially dead

3. How to Fetch Dev.to Articles for Your Portfolio

Integrate the articles of your Dev.to profile into your personal portfolio with either React, Vue, or Next.js by following these simple steps. It outlines how to include frontend to pull the information and correctly utilizes the Dev.to API: How to Fetch Your Dev.to Articles for Your Portfolio with React