简体   繁体   English

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

[英]How to add a custom geolocate me button in Mapbox GL JS?

I am trying to add a custom Geolocate me button to my map and it kind of works, however only if I also add the standard icon from Mapbox.我正在尝试将自定义 Geolocate me 按钮添加到我的 map 并且它可以工作,但前提是我还从 Mapbox 添加标准图标。 The code below works, but if I remove the line map.addControl(geolocate, 'top-right');下面的代码有效,但如果我删除行map.addControl(geolocate, 'top-right'); , my left button stops working. ,我的左键停止工作。

      // 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')

screenshot - in blue is what I want to keep, in Red to remove屏幕截图- 蓝色是我要保留的内容,红色是要删除的内容

Instead of creating new mapboxgl.GeolocateControl Instance, you can extend mapboxgl.GeolocateControl Class like below:您可以像下面这样扩展 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')

Other Helpful links:其他有用的链接:

You can easily add custom button in mapBox.您可以轻松地在 mapBox 中添加自定义按钮。 在此处输入图像描述

add some css:添加一些 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;}

add the button添加按钮

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

Add your code to your button将代码添加到按钮

$('.map_btn').click(function(e)
{
    app.preloader.show();
    getLocation() ;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM