2

I am very new to React and leaflet. I am trying to plot a set of latitudes and longitudes that is available in an object on a map in React using leaflet. Any pointers on how this can be done will be helpful.

I have gone through tutorials on react leaflet from https://react-leaflet.js.org. But I am unable to proceed. Any help is greatly appreciated. Thanks in advance.

Sample of the array of object data I have:

data=[
  {
    from_lat: "12.92415",
    from_long: "77.67229",
    id: "132512",
    to_lat: "12.92732",
    to_long: "77.63575",
  },
  {
    from_lat: "12.96691",
    from_long: "77.74935",
    id: "132513",
    to_lat: "12.92768",
    to_long: "77.62664",
  }
]
3
  • from what i have gathered so far, I am supposed to use react-leaflet. but i am unsure what methods/functions will give me the desired results. Commented May 27, 2019 at 13:43
  • Ok. I saw in the description that you do use react-leaflet. What exactly do you mean by plot? Do you want to visualize the points as markers or as poly-lines for instance as I see there is a start and an end?
    – kboul
    Commented May 27, 2019 at 13:46
  • not markers, the polyline is what i need, yes. Commented May 27, 2019 at 13:52

2 Answers 2

8

You can store the data in the state like this:

state = {
    ...
    data: [
      {
        from_lat: "12.92415",
        from_long: "77.67229",
        id: "132512",
        to_lat: "12.92732",
        to_long: "77.63575",
      },
      {
        from_lat: "12.96691",
        from_long: "77.74935",
        id: "132513",
        to_lat: "12.92768",
        to_long: "77.62664",
      }
    ]
  };

and then iterate over data and return a Polyline instance like this:

<Map
      style={{ height: "100vh" }}
      center={position}
      zoom={this.state.zoom}
    >
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
      {this.state.data.map(({id, from_lat, from_long, to_lat, to_long}) => {
        return <Polyline key={id} positions={[
          [from_lat, from_long], [to_lat, to_long],
        ]} color={'red'} />
      })}
    </Map>

Demo

0
3

https://codesandbox.io/s/react-leaflet-polyline-hedzz

  1. There is a position array where contains lat lng array.
  2. Take MapContainer if react-leaflet version is > 2.8.0 Otherwise Map.
  3. Take Marker and Take Polyline inside the FeatureGroup
  4. TileLayer is needed to show the map inside div
  5. You can make Other component for Icon Otherwise define it in same component Now you are good to go ...
import {
  FeatureGroup,
  MapContainer,
  Marker,
  Polyline,
  TileLayer
} from "react-leaflet";
import "leaflet/dist/leaflet.css";
import L from "leaflet";

const CustomPoliLine = () => {
  const mapRef = useRef();
  const [center, setCenter] = useState({ lat: 36.460353, lng: 126.440674 });
  const [map, setMap] = useState(null);

  const pos = [
    [36.460353, 126.440674],
    [34.789594, 135.438084], //to jpn
    [36.460353, 126.440674],
    [55.410343, 37.902312], //to rus
    [36.460353, 126.440674],
    [40.085148, 116.552407] //to chi
  ];

  return (
    <div>
      <MapContainer
        style={{ height: "480px", width: "100%" }}
        zoom={6}
        center={center}
        ref={mapRef}
        whenReady={setMap}
        scrollWheelZoom={true}
      >
        <FeatureGroup>
          {pos?.map((mark, i) => (
            <Marker key={i} position={mark} icon={customMarkerUserPos} />
          ))}

          <Polyline positions={pos} color="red" />
        </FeatureGroup>

        <TileLayer url="http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
      </MapContainer>
    </div>
  );
};

export default CustomPoliLine;



export const customMarkerUserPos = new L.Icon({
  iconUrl: "https://unpkg.com/[email protected]/dist/images/marker-icon.png",
  iconSize: [15, 20],
  iconAnchor: [5, 20],
  popupAnchor: [2, -40]
});

3
  • 4
    Please never post link-only answers. If you want to drop a hyperlink, post a comment under the question. If that link dies, so does any value in this answer. Commented Dec 9, 2021 at 10:42
  • now posted an answar below the link .
    – manoj garu
    Commented Dec 9, 2021 at 11:35
  • Now the only thing missing is the part where you explain the snippet and try to educate the OP (and thousands of future researchers) on how/why your snippet resolves the question. (I did not dv your answer). Commented Dec 9, 2021 at 12:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.