首页 > 解决方案 > 搜索时使用 Leaflet Geocoder 而不设置标记

问题描述

目前我正在使用传单地图并添加了这个地理编码器插件:https ://github.com/perliedman/leaflet-control-geocoder 。我的问题是,每次我搜索一个地方时,它也会在这个位置设置一个标记,但我不希望它这样做。它应该只放大而不设置标记。

有人知道如何禁用此功能或立即删除设置标记吗?

设置标记看起来像这样

我在 Ionic/typescript 中工作,我使用地理编码器的代码是这样的:

leaflet.Control.geocoder().addTo(this.map);   

根据 GitHub,defaultMarkGeocode:false 应该禁用标记。使用它时,我只是收到此错误:

无法读取未定义的属性“_leaflet_id”

我也试过

var geocoder = L.Control.geocoder({
    defaultMarkGeocode: false
})
.on('markgeocode', function(e) {
    var bbox = e.geocode.bbox;
    var poly = L.polygon([
         bbox.getSouthEast(),
         bbox.getNorthEast(),
         bbox.getNorthWest(),
         bbox.getSouthWest()
    ]).addTo(map);
    map.fitBounds(poly.getBounds());
})
.addTo(map);

但它只是告诉我变量地理编码器从未使用过,我收到此错误:

无法读取未定义的属性“addLayer”

如果您对如何修复它以及禁用或删除此标记有任何建议,我将不胜感激。

问候

标签: typescriptionic-frameworkionic3leafletgeocoder

解决方案


Could not reproduce the exact error messages you describe:

var map = L.map('map').setView([0, 0], 2);

var geocoder = L.Control.geocoder({
    defaultMarkGeocode: false,
    collapsed: false
  })
  .on('markgeocode', function(e) {
    var bbox = e.geocode.bbox;
    var poly = L.polygon([
      bbox.getSouthEast(),
      bbox.getNorthEast(),
      bbox.getNorthWest(),
      bbox.getSouthWest()
    ]).addTo(map);
    map.fitBounds(poly.getBounds());
  })
  .addTo(map);

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
html,
body,
#map {
  height: 100%;
  margin: 0;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ==" crossorigin="" />
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet-src.js" integrity="sha512-IkGU/uDhB9u9F8k+2OsA6XXoowIhOuQL1NTgNZHY1nkURnqEGlDZq3GsfmdJdKFe1k1zOc6YU2K7qY+hF9AodA==" crossorigin=""></script>

<link rel="stylesheet" href="https://unpkg.com/leaflet-control-geocoder@1.5.8/dist/Control.Geocoder.css" />
<script src="https://unpkg.com/leaflet-control-geocoder@1.5.8/dist/Control.Geocoder.js"></script>

<div id="map"></div>

However when collapsed option is set to false, there is another error message:

TypeError: this.options.geocoder[mode] is not a function

…which is solved in plugin repo by PR perliedman/leaflet-control-geocoder#184, but it is not yet shipped in a released version in npm / unpkg CDN.

If you need further help on your error messages, please provide code that reproduces them.


推荐阅读