5

我正在尝试向我的地图添加一个自定义的 Geolocate me 按钮,它有点工作,但前提是我还添加了 Mapbox 中的标准图标。下面的代码有效,但如果我删除 line 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')
      el.className = className;
      el.textContent = 'Use my location';
      el.addEventListener('click', (e) => {
      geolocate.trigger();
      }, false)
      return el;
    }
  }
  const toggleControl = new ToggleControl({
    className: 'mapboxgl-ctrl'
  })
  map.addControl(toggleControl, 'top-left')

屏幕截图- 蓝色是我要保留的内容,红色是要删除的

4

2 回答 2

3

您可以扩展 mapboxgl.GeolocateControl 类,而不是创建新的 mapboxgl.GeolocateControl 实例,如下所示:

class ToggleControl extends mapboxgl.GeolocateControl {
            _onSuccess(position) {
                this.map.flyTo({
                    center: [position.coords.longitude, position.coords.latitude],
                    zoom: 17,
                    bearing: 0,
                    pitch: 0
                });
            }

            onAdd(map, cs) {
                this.map = map;
                this.container = document.createElement('div');
                this.container.className = `mapboxgl-ctrl`;
                const button = this._createButton('monitor_button')
                this.container.appendChild(button);
                return this.container;
            }

            _createButton(className) {
                const el = window.document.createElement('button')
                el.className = className;
                el.textContent = 'Use my location';
                el.addEventListener('click', () => {
                    this.trigger();
                });
                this._setup = true;
                return el;
            }
        }
        const toggleControl = new ToggleControl({})
        map.addControl(toggleControl, 'top-left')

其他有用的链接:

于 2020-09-14T17:36:39.727 回答
0

您可以轻松地在 mapBox 中添加自定义按钮。在此处输入图像描述

添加一些CSS:

    .map_btn{position: absolute; top: 25;  width: 36px;height:36px;border-radius:50%;border:none;float:right;margin-right:5px;zindex:111;left:85%;background:#e8faa7;}

添加按钮

               <button class="map_btn"><img src="loc1.png" width=30/> 
               </button>
                <div id="map"></div>

将代码添加到按钮

$('.map_btn').click(function(e)
{
    app.preloader.show();
    getLocation() ;
});
于 2022-03-02T08:21:38.843 回答