小编Ang*_*huk的帖子

如何在 Mapbox GL JS 中添加自定义地理定位我按钮?

我正在尝试向我的地图添加一个自定义的 Geolocate me 按钮并且它有点工作,但是前提是我还添加了来自 Mapbox 的标准图标。下面的代码有效,但如果我删除该行map.addControl(geolocate, 'top-right');,我的左按钮将停止工作。

      // Initialize the geolocate control.
  var geolocate = new mapboxgl.GeolocateControl({
    positionOptions: {
      enableHighAccuracy: true
    },
      trackUserLocation: true
    });
    // Add the control to the map.
  map.addControl(geolocate, 'top-right');

  class ToggleControl {

    constructor(options) {
      this._options = Object.assign({}, this._options, options)
    }

    onAdd(map, cs) {
      this.map = map;
      this.container = document.createElement('div');
      this.container.className = `${this._options.className}`;

      const button = this._createButton('monitor_button')
      this.container.appendChild(button);
      return this.container;
    }
    onRemove() {
      this.container.parentNode.removeChild(this.container);
      this.map = undefined;
    }
    _createButton(className) {
      const el = window.document.createElement('button') …
Run Code Online (Sandbox Code Playgroud)

javascript mapbox mapbox-gl-js icontrol

5
推荐指数
1
解决办法
456
查看次数

标签 统计

icontrol ×1

javascript ×1

mapbox ×1

mapbox-gl-js ×1