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

7

我正在尝试在我的地图上添加一个自定义的“定位我”按钮,它有点起作用,但仅当我还添加了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')
      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')

截图 - 蓝色部分是我想保留的内容,红色部分需要删除

2个回答

5

不需要创建新的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')

其他有用的链接:


0
你可以在 MapBox 中轻松添加自定义按钮。 enter image description here 添加一些 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() ;
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接