Import leaftlet-control-geocoder in Angular 8 Project?

huangapple go评论127阅读模式
英文:

Import leaftlet-control-geocoder in Angular 8 Project?

问题

以下是翻译好的部分:

  1. Angular版本:8
  2. package.json中的版本:
  3. "leaflet": "1.5.1",
  4. "leaflet-draw": "1.0.4",
  5. "leaflet-sidebar-v2": "3.0.3",
  6. "esri-leaflet": "2.3.2",
  7. "leaflet-control-geocoder": "1.10.0"
  8. 我已经像这样导入了leaflet和其他内容:

import * as L from 'leaflet';
import 'leaflet-draw';
import * as esri from 'esri-leaflet';
import 'leaflet-sidebar-v2';
import 'leaflet-control-geocoder';

  1. 现在我需要使用leaflet-control-geocoder来实现这个功能,但如果我写这个:

L.control.geocoder().addTo(map);

  1. Angular会崩溃(属性'geocoder'在类型'typeof Control'上不存在)。
  2. 我也尝试了esri-leaflet-geocoder,但它也崩溃了。
  3. 有解决方案吗?
英文:

angular verison: 8

version in package.json:

  1. "leaflet": "1.5.1",
  2. "leaflet-draw": "1.0.4",
  3. "leaflet-sidebar-v2": "3.0.3",
  4. "esri-leaflet": "2.3.2",
  5. "leaflet-control-geocoder": "1.10.0"

I have import leaflet and other like this:

  1. import * as L from 'leaflet';
  2. import 'leaflet-draw';
  3. import * as esri from 'esri-leaflet';
  4. import 'leaflet-sidebar-v2';
  5. import 'leaflet-control-geocoder';

Now i have to use or leaflet-control-geocoder to implement this function but if i write this:

  1. L.control.geocoder().addTo(map);

angular crash.(Property 'geocoder' does not exist on type 'typeof Control')
I try also with esri-leaflet-geocoder, but it's crash too.

any solution?

答案1

得分: 3

我遇到了相同的问题。我通过以下方式解决了它

  1. import * as L from 'leaflet';
  2. import Geocoder from 'leaflet-control-geocoder';
  3. // ...
  4. this.map = L.map('map', {
  5. center: [staticLatLng.lat, staticLatLng.lng],
  6. zoom: zoomLevel
  7. });
  8. const GeocoderControl = new Geocoder();
  9. GeocoderControl.addTo(this.map);
  10. GeocoderControl.on('markgeocode', function (e) {
  11. console.log(e);
  12. });
  13. // ...
英文:

I had the same problem. I fixed it by doing so

  1. import * as L from 'leaflet';
  2. import Geocoder from 'leaflet-control-geocoder';
  3. ...
  4. this.map = L.map('map', {
  5. center: [staticLatLng.lat, staticLatLng.lng],
  6. zoom: zoomLevel
  7. });
  8. const GeocoderControl = new Geocoder();
  9. GeocoderControl.addTo(this.map);
  10. GeocoderControl.on('markgeocode', function (e) {
  11. console.log(e);
  12. });
  13. ...

答案2

得分: 0

请尝试使用大写字母 "C", L.Control.geocoder().addTo(map)

或者(但效果应该相同)

  1. var geocoder = new L.Control.geocoder();
  2. map.addControl(geocoder);
英文:

Pls try with uppercase "C" L.Control.geocoder().addTo(map)

or (but that should have the same effect)

  1. var geocoder = new L.Control.geocoder();
  2. map.addControl(geocoder);

答案3

得分: 0

我遇到了相同的问题。我将geocoder添加到L.Routing.control内部,它可以工作!(实际上,VS Code仍然显示“属性'Geocoder'在'typeof Control'上不存在”),但它可以工作!:

  1. L.Routing.control({
  2. waypoints: [L.latLng(57.74, 11.94), L.latLng(57.6792, 11.949)],
  3. showAlternatives: true,
  4. geocoder: L.Control.Geocoder.nominatim()
  5. }).addTo(this.map);

我正在使用Angular 8和leaftlet-routing-machine插件。

英文:

I had the same problem. I added the geocoder inside L.Routing.control, and it works! (actually, VS Code still says Property 'Geocoder' does not exist on type 'typeof Control') but it works!:

L.Routing.control({
waypoints: [L.latLng(57.74, 11.94), L.latLng(57.6792, 11.949)],
showAlternatives: true,
geocoder: L.Control.Geocoder.nominatim()
}).addTo(this.map);

I'm working with Angular 8, leaftlet-routing-machine plugin.

huangapple
  • 本文由 发表于 2020年1月3日 16:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定