angular - 如何在 Angular 12.x 中导入传单控制地理编码器?
问题描述
我已经使用以下命令在 Angular 12 中安装了传单:
npm install leaflet
npm install @asymmetrik/ngx-leaflet
npm install --save-dev @types/leaflet
我包括了样式:./node_modules/leaflet/dist/leaflet.css
在angular.json
文件上和LeafletModule
没有我的app.module.ts
.
该地图工作正常,但是,我正在尝试从地理编码器添加搜索输入控件,因为我已将地理编码器链接的链接和脚本包含到我的 index.html 中,可在此处找到。
这是我的 ts 文件:
import { Component, AfterViewInit } from '@angular/core';
import * as L from 'leaflet';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements AfterViewInit {
title = 'leaf-let';
private map: any;
constructor() {}
ngAfterViewInit(): void {
this.initMap();
}
private initMap(): void {
this.map = L.map('map').setView([14.094167, -87.206667], 15);
const tiles = L.tileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{
maxZoom: 19,
attribution:'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> `
`contributors',
}
);
tiles.addTo(this.map);
//L.Control.geocoder().addTo(this.map);
}
但是当我这样做时,L.Control.geocoder().addTo(this.map);
它给了我错误:
“typeof control”类型上不存在属性“geocoder”
解决方案
您尚未导入库及其样式:
- 安装传单控制地理编码器
import "leaflet-control-geocoder/dist/Control.Geocoder.css";
import "leaflet-control-geocoder/dist/Control.Geocoder.js";
此外,您似乎没有使用 ngx-leaflet 而是使用 vanilla 传单。如果是这种情况,您应该采用以下方法:
private initMap(): void {
this.map = L.map("map").setView([14.094167, -87.206667], 15);
const tiles = L.tileLayer(
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
{
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}
).addTo(this.map);
tiles.addTo(this.map);
(L.Control as any).geocoder().addTo(this.map);
}
使用初始化插件(L.Control as any)
以避免收到打字稿错误,但您收到的错误是因为您没有提前导入库。
推荐阅读
- discord.js - 机器人名称作为前缀
- python - 尝试拟合回归模型时出现 ValueError
- android - 在 Google Play 商店中搜索时未显示已发布的应用
- git - Git:如何删除与存储库中任何现有文件无关的所有提交
- java - 随着项目的增加,连接的数量也在增加
- dart - 如何使用 build_runner 在 dart2 中导入生成的源代码?
- regex - 在 \N 内插入变量
- sql-server - sqlcmd脚本变量理解问题
- java - 为什么我们在接口的实现名称前加上前缀而不是后缀?
- android - Firebase 数据消息到特定的 android 应用程序版本