React Locate on map

给你一囗甜甜゛ 提交于 2019-12-04 17:16:12

react-leaflet-locate-control package is not compatible with the latest version (v2) of react-leaflet and in fact the similar issue has been reported here

Since react-leaflet-locate-control represents a wrapper for leaflet-locatecontrol plugin, the following custom component for react-leaflet could be utilized instead which offers the same functionality as react-leaflet-locate-control:

import React, { Component } from "react";
import { withLeaflet } from "react-leaflet";
import Locate from "leaflet.locatecontrol";

class LocateControl extends Component {
  componentDidMount() {
    const { options, startDirectly } = this.props;
    const { map } = this.props.leaflet;

    const lc = new Locate(options);
    lc.addTo(map);

    if (startDirectly) {
      // request location update and set location
      lc.start();
    }
  }

  render() {
    return null;
  }
}

export default withLeaflet(LocateControl);

Installation

1) install a plugin: npm install leaflet.locatecontrol

2) include the following CSS resources into public/index.html:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet.locatecontrol/dist/L.Control.Locate.min.css">

Here is a demo (source code)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!