Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Locate on map

I would like to add the module "React-leaflet-locate-control" on Map. Unfortunately, I have this error "TypeError: Cannot read property 'addLayer' of undefined" and I don't know how to correct this error.

Can you help me please ?

Here is my component Map :

import './Map.css';
import React, { Component } from 'react';
import { Map, TileLayer, Marker, Popup } from 'react-leaflet';
import L from "leaflet";
import { getLat, getLng } from '../../Store.js';
import SearchBar from '../SearchBar/SearchBar.js';
import LocateControl from 'react-leaflet-locate-control';

const customMarker = new L.icon({
    iconUrl: "https://unpkg.com/[email protected]/dist/images/marker-icon.png",
    iconSize: [25, 41],
    iconAnchor: [13, 0]
});

export default class MapLeaflet extends Component {

    constructor(props) {
        super(props);
        this.state = {
            lat: getLat(),
            lng: getLng(),
        }
    }

    updateMarker = (e) => {
        this.props.updateMarkerPosition(e.latlng.lat, e.latlng.lng);
        this.setState({
            lat: e.latlng.lat,
            lng: e.latlng.lng
        })
    }

    render() {
        const position = [this.state.lat, this.state.lng]
        const locateOptions = {
            position: 'topright',
            strings: {
                title: 'Show me where I am, yo!'
            },
            onActivate: () => {} // callback before engine starts retrieving locations
        }
        return (
            <div className="map">
                <Map center={position} zoom={13} className="map" onClick={this.updateMarker}>
                    <TileLayer
                        attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                    />
                    <Marker position={position} icon={customMarker}>
                        <Popup>
                            A pretty CSS3 popup. <br /> Easily customizable.
                        </Popup>
                    </Marker>
                    <SearchBar />
                    <LocateControl options={locateOptions} startDirectly/>
                </Map>
            </div>
        )
    }
}
like image 521
Kloni Avatar asked Jan 08 '19 21:01

Kloni


People also ask

What is bootstrapURLKeys?

bootstrapURLKeys is an object that holds the API key you copied from your Google Console. Now you can hardcode the key here, but that is not recommended for code that gets committed to GitHub or is otherwise publicly accessible. You can check out this discussion on how to secure your API keys on the client.

Is Google Map API free?

Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).


1 Answers

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)

like image 145
Vadim Gremyachev Avatar answered Oct 04 '22 20:10

Vadim Gremyachev