react-mapbox-gl _onClickGeolocate() call with setTimeout to request location automatically

问题内容:

I am using the following code to add the GeolocateControl to my react-mapbox-gl map in react.

I need to automatically call the method _onClickGeolocate method using setTimeout() to request the user location automatically upon loading the page.

How would I be able to achieve this ?

import { Component } from "react";
import PropTypes from "prop-types";
import { accessToken } from "../../api/tokens/mapbox";
import mapboxgl from "mapbox-gl";

class Locater extends Component {
  static contextTypes = { map: PropTypes.object.isRequired };
  componentDidMount() {
    const { map } = this.context;

    map.addControl(
      new mapboxgl.GeolocateControl({
        accessToken,
        positionOptions: {
          enableHighAccuracy: true
        },
        trackUserLocation: true
      })
    );
  }

  render() {
    return null;
  }
}

export default Locater;

问题评论:

原文地址:

https://stackoverflow.com/questions/47756027/react-mapbox-gl-onclickgeolocate-call-with-settimeout-to-request-location-aut

添加评论