Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with react-leaflet v2 #6

Open
jayenashar opened this issue Aug 30, 2018 · 7 comments
Open

Doesn't work with react-leaflet v2 #6

jayenashar opened this issue Aug 30, 2018 · 7 comments

Comments

@jayenashar
Copy link

Seems context has changed quite a bit.

@jayenashar
Copy link
Author

Managed to use this (no idea why i need the setTimeout):

import {MapControl, withLeaflet} from 'react-leaflet';

class LocateControlImpl extends MapControl {
    createLeafletElement(props) {
        const {options, startDirectly} = props;
        const {map} = props.leaflet;
        const lc = L.control.locate(options).addTo(map);

        if (startDirectly)
            setTimeout(() => {
                lc.start()
            }, 0);
        return lc;
    }
}

const LocateControl = withLeaflet(LocateControlImpl);

@chuikoffru
Copy link

I also have this error...

TypeError: Cannot read property 'addLayer' of undefined

2019-01-08 9 41 30

@cyrildurand
Copy link

I had the same issue and fix it like this :

import L from 'leaflet';
import { withLeaflet, MapControl } from 'react-leaflet';
import 'leaflet.locatecontrol';

class LocateControl extends MapControl {
  createLeafletElement(props) {
    const {
      leaflet: { map },
      ...options
    } = props;

    const lc = L.control.locate(options).addTo(map);
    return lc;
  }
}
export default withLeaflet(LocateControl);

I also extend it for my purpose

import L from 'leaflet';
import { withLeaflet, MapControl } from 'react-leaflet';
import 'leaflet.locatecontrol';
import ReactDOMServer from 'react-dom/server';

class LocateControl extends MapControl {
  createLeafletElement(props) {
    const {
      leaflet: { map },
      ...options
    } = props;

    if (this.props.children !== undefined && options.createButtonCallback === undefined) {
      options.createButtonCallback = (container, options) => {
        let link = L.DomUtil.create('a', 'leaflet-bar-part leaflet-bar-part-single', container);
        link.role = 'button';
        link.href = '#';
        link.title = options.strings.title;
        link.innerHTML = ReactDOMServer.renderToString(this.props.children);

        return { link, icon: link.firstChild };
      };
    }

    const lc = L.control.locate(options).addTo(map);
    return lc;
  }
}

export default withLeaflet(LocateControl);

which allow me to put my own svg icon

          <LocateControl flyTo>
            <Icon color="#000" icon={ICONS.LOCATE_ME} />
          </LocateControl>

Not sure how to check for v2 of leaflet but let me know if you want me to create a pull request

@MudassarAther
Copy link

@cyrildurand It doesn't work for current version of react-leaflet. Could you let me know which version you used?

@cyrildurand
Copy link

It works with react-leaflet v2.2.0, leaflet v1.4.0 and leaflet.locatecontrol v0.66.0.

Do you have any error message ?

@MudassarAther
Copy link

Now, I see it works. Seems like the reason was at other codes. Thanks for your support.

@fgrs
Copy link

fgrs commented Feb 21, 2019

https://react-leaflet.js.org/docs/en/plugins.html still says not supported in v2 leaflet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants