Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Drawing lines between Markers using Google-Map-React

I'd like to draw simple straight lines between multiple markers on Google Map. I render this map using google-map-react.

Map component

import { useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import GoogleMapReact from 'google-map-react';
import Marker from './marker';

export default function Map() {

    const dispatch = useDispatch();
    const markers = useSelector(state => state.points);
    const [draggable, setDraggable] = useState(true);
    
    const [center, setCenter] = useState({
        lat: 59.95,
        lng: 30.33
      });
    const [zoom, setZoom] = useState(14);

    const moveMarker = (key, childProps, mouse) => {
        let markersCopy = markers;
        markersCopy.map(e => e.currPoint === key ? e.currCenter = mouse : e);
        dispatch({type: 'REORDER', payload: markersCopy});
    }

    return (
        <div style={{ height: '100vh', width: '100%' }}>
            <GoogleMapReact
                bootstrapURLKeys={{ key: process.env.REACT_APP_GOOGLE_MAPS_API_KEY }}
                defaultCenter={center}
                defaultZoom={zoom}
                onChange={(e) => dispatch({type: 'CHANGE_CURR_CENTER', payload: e.center})}
                draggable={draggable}
                onChildMouseDown={() => setDraggable(false)}
                onChildMouseUp={() => setDraggable(true)}
                onChildMouseMove={(key, childProps, mouse) => moveMarker(key, childProps, mouse)}
                >
                {markers.map(e => {
                    return (
                        <Marker
                            lat={e.currCenter.lat}
                            lng={e.currCenter.lng}
                            text={e.currPoint}
                            key={e.currPoint}
                        />
                    )
                })}
            </GoogleMapReact>
      </div>
    
    )
}

I'd like to have them connected by straight lines in the order in which they are on the list (I'm adding them to the list manually in another component). The resulting line should represent the route, the first point in the list is the beginning of the route, the last one is the end of the route. How can I achieve this?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!