【问题标题】:Leaflet Esri geocoding on Angular not import Geocoding classAngular上的传单Esri地理编码不导入地理编码类
【发布时间】:2021-01-04 01:22:10
【问题描述】:

我尝试在 Angular 项目中使用地理编码表单 esri-leaflet 库,但我遇到了导入类问题。

这是我的组件代码:

import { Component, OnInit, AfterViewInit, Type } from '@angular/core';
import { latLng, tileLayer, layerGroup, marker, Layer, Control, circle, polygon, Map } from 'leaflet';
import * as L from 'leaflet';
import * as esri from 'esri-leaflet';
import GeocodeService from 'esri-leaflet-geocoder/dist/esri-leaflet-geocoder';


@Component({
  selector: 'abd-map',
  templateUrl: './abd-map.component.html',
  styleUrls: ['./abd-map.component.scss']
})
export class AbdMapComponent implements OnInit, AfterViewInit {


  constructor() {}

  ngOnInit(): void {

 }


  ngAfterViewInit(): void{


    const openStreetMap = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    });

    const googleMap = L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
        maxZoom: 20,
        subdomains: ['mt0', 'mt1', 'mt2', 'mt3']
    });


    const map = L.map('map', {
        center: [39.61, -105.02],
        zoom: 13,
        layers: [openStreetMap, googleMap]
    });

    const baseMaps = {
      'openStreetMap': openStreetMap,
      'googleMap': googleMap
  };

    L.control.layers(baseMaps).addTo(map);


    const searchControl = esri.Geocoding.geosearch().addTo(map);


  }



}

当我运行“ng serve”时出现这个错误:

./src/app/abd-map/abd-map.component.ts 41:30-44 中的错误 “在“esri-leaflet”中找不到导出“地理编码”(导入为“esri”)

有人可以帮助我吗? 谢谢。

【问题讨论】:

    标签: angular typescript leaflet esri esri-leaflet-geocoder


    【解决方案1】:

    你应该导入:

    import "leaflet/dist/leaflet.css";
    import * as L from "leaflet";
    import "esri-leaflet-geocoder/dist/esri-leaflet-geocoder.css";
    import "esri-leaflet-geocoder/dist/esri-leaflet-geocoder";
    import * as ELG from "esri-leaflet-geocoder";
    

    然后像这样初始化插件:

      const searchControl = new ELG.Geosearch();
      const results = new L.LayerGroup().addTo(map);
    
        searchControl
          .on("results", function (data) {
            results.clearLayers();
            for (let i = data.results.length - 1; i >= 0; i--) {
              results.addLayer(L.marker(data.results[i].latlng));
            }
          })
          .addTo(map);
    

    Demo

    【讨论】:

    • 好的,谢谢,现在通过这个导入,我的代码可以工作了,但我也遇到了反向地理编码的问题。常量 geocodeService = esriGeocoder.geocodeService(); map.on('click', e => { 调试器 geocodeService.reverse().latlng(e.latlng).run(res => { }); });错误 TS2339:“LeafletEvent”类型上不存在属性“latlng”。
    • 那是我朋友的另一个问题。请在新线程中提问,如果对您有帮助,请接受/投票。
    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多