ngx-leaflet/Angular 2 中具有下拉选择输入的属性绑定映射单击事件

2024-04-30

我有一张带有区域多边形的地图。在地图下方,我有一个下拉选择输入,可以动态读取多边形名称作为选项。当用户单击地图上的区域多边形时,我希望下拉选择输入使用选定的多边形名称(作为 geojson 文件中的“名称”字段存在)进行更新。

我认为实现此目的的方法是在 HTML 模板文件中使用属性绑定。因此,在我的选择类中,我将 value 属性设置为值clicked,像这样:

<select class="form-control" id="selectRegion" [value]="clicked">

clicked最初在应用程序组件中定义为空字符串。当单击多边形时,我设置clicked作为多边形名称字段内onEachFeature单击事件。控制台日志显示clicked变量正在正确更新。但是,选择输入不会按预期随单击事件更新。

我怀疑问题在于this我的函数内部没有正确更新吗?如何获取选择输入以通过我的地图点击事件进行更新?

这是一些代码(基于 Asymmetrik 的 ngx-leaflet-tutorial-ngcli):

Geojson(另存为polygons.geojson在资产文件夹中:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "stroke": "#555555",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "fill": "#555555",
        "fill-opacity": 0.5,
        "name": "poly1"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -121.95098876953125,
              46.82966386051541
            ],
            [
              -121.78482055664061,
              46.82966386051541
            ],
            [
              -121.78482055664061,
              46.91368905872705
            ],
            [
              -121.95098876953125,
              46.91368905872705
            ],
            [
              -121.95098876953125,
              46.82966386051541
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "stroke": "#555555",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "fill": "#555555",
        "fill-opacity": 0.5,
        "name": "poly2"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -121.77726745605469,
              46.83107318799318
            ],
            [
              -121.62963867187499,
              46.83107318799318
            ],
            [
              -121.62963867187499,
              46.913220009605624
            ],
            [
              -121.77726745605469,
              46.913220009605624
            ],
            [
              -121.77726745605469,
              46.83107318799318
            ]
          ]
        ]
      }
    }
  ]
}

应用程序模块.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { LeafletModule } from '@asymmetrik/ngx-leaflet';
import { HttpClientModule } from '@angular/common/http';


import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    LeafletModule.forRoot(),
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

应用程序组件.html:

<div class="map"
  leaflet
  [leafletLayers]="layers"
     [leafletFitBounds]="fitBounds"></div>
<div class="form-group">
  <select class="form-control" id="selectRegion" [value] = "clicked">
    <option *ngFor="let region of regions">{{ region }}</option>
  </select>
</div>

应用程序组件.ts:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import * as L from 'leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  layers: L.Layer[];
  fitBounds = [[46.67, -122.25], [47.01, -121.302]];
  regions = [];
  clicked = '';

  constructor(private http: HttpClient) { }

  ngOnInit() {

    // read in geojson of poly
    this.http.get<any>('/assets/polygons.geojson')
      .subscribe(poly => {

        const tileLayer = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}.png', {
          subdomains: 'abcd',
          maxZoom: 19
        });

        const polyLayer = L.geoJSON(poly, {
          onEachFeature: this.onEachFeature.bind(this)
        });

        this.layers = [ tileLayer, polyLayer ];
        console.log(this);
      });
  }

  // loop through each feature of polyLayer
  onEachFeature(feature, layer) {

    // push polygon names to regions array
    this.regions.push(feature.properties.name);

    layer.on('click', <LeafletMouseEvent> (e) => {
      this.clicked = e.target.feature.properties.name;
      console.log(this.clicked);
    });
  }
}

The onEachFeature功能和layer.on('click'...事件回调可能在 Angular 区域之外被调用。如果是这种情况,则更改检测将不会自动起作用。您应该将更改包含在zone.run()调用以确保您的更改是在 Angular 区域中进行的 - 如本示例所示:

// loop through each feature of polyLayer
onEachFeature(feature, layer) {

  this.zone.run(() => {
    // push polygon names to regions array
    this.regions.push(feature.properties.name);

    layer.on('click', <LeafletMouseEvent> (e) => {
      this.zone.run(() => {
        this.clicked = e.target.feature.properties.name;
        console.log(this.clicked);
      }
    });
  }

}

您可以按照这些指示(https://github.com/Asymmetrik/ngx-leaflet#a-note-about-change-detection https://github.com/Asymmetrik/ngx-leaflet#a-note-about-change-detection)来看看它是如何工作的并将 ngZone 实例注入到您的组件中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ngx-leaflet/Angular 2 中具有下拉选择输入的属性绑定映射单击事件 的相关文章

随机推荐

  • AngularJS ui.router 更改页面并转到特定部分

    我使用 AngularJSui router在我的应用程序中 对于新的用例 我想更改视图index html到我的partialview options html 我的options html有不同的部分 不同视图之间的路由工作正常 但我想
  • 在螺旋线上画等距点

    我需要一种算法来计算螺旋路径上的点的分布 该算法的输入参数应为 环路宽度 距最内环路的距离 点之间的距离固定 绘制点数 要绘制的螺旋是阿基米德螺线并且获得的积分必须是等距离来自彼此 该算法应该打印出单点的笛卡尔坐标序列 例如 点 1 0 0
  • 如何有效地将多个 pandas 列组合成一个类似数组的列?

    使用对象类型列之类的东西创建 或加载 DataFrame 很容易 如下所示 In pdf pd DataFrame a 1 2 3 b 4 5 6 c 7 8 9 combined 1 4 7 2 5 8 3 6 9 Out a b c c
  • 在 WSL2 中通过 IDE 连接到 kafka 服务器时出错

    我无法通过在 Windows 上运行的 intellij 或 vscode 连接到在 ubuntu 上运行的 kafka 服务器 我在 WSL2 上尝试的第一个服务器 我什至尝试使用虚拟机的IP 但没有成功 据我了解 我们应该能够根据此文档
  • 如何更改 Oracle 云实例的公钥?

    我不小心删除了我的公钥和私钥 并且由于无法恢复密钥 并且在任何地方都没有备份 而不得不生成新的 SSH 密钥 如何更改 Oracle Cloud 实例的公共 SSH 密钥 终止实例并重新创建它不是一个选择 我尝试在网上查找但找不到太多内容
  • 何时处置 CancellationTokenSource?

    班上CancellationTokenSource是一次性的 快速查看 Reflector 证明了KernelEvent 很可能 非托管资源 自从CancellationTokenSource没有终结器 如果我们不处理它 GC就不会这样做
  • 以编程方式创建标签[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我是一名 iPhone 开发新手 我想以编程方式创建一个 UILabel 并且我想了解 UILabel 类的所有属性和功能 UILab
  • Cocoapods 彻底崩溃

    首先 我正在使用 Cocoapods 0 37 2 XCode 6 3 1 为 iOS 8 3 进行编译 在我运行基本的 pod update 之前 我的项目运行得很好 现在它完全拒绝编译 我非常肯定这与豆荚本身无关 具体来说 我收到的错误
  • Spring - 属性“name”不允许出现在元素“constructor-arg”中

    我在程序中使用 hsqldb 作为数据库 我想通过 spring 注入构造函数值 这是我的豆子
  • Symfony 从控制器设置块内容

    有没有办法在 Symfony 的控制器中设置模板块内容 有没有办法在控制器中执行类似的操作 this gt get templating gt setBlockContent page title page title 我需要动态设置页面标
  • Python:文本覆盖在所有窗口顶部,包括 Linux 中的全屏

    我正在尝试用 python 编写一个简单的脚本 在所有窗口和全屏应用程序之上输出文本 该脚本的目的是以类似于 Steam FPS 计数器工作方式的方式输出平均负载和可用内存 以及其他有用的统计数据 到目前为止 我尝试了 pygame 但据我
  • GCM 卷曲操作超时

    我的服务器上存储了几个负责 GCM 操作的 php 文件 它们似乎在需要时工作得很好 但它们经常返回一个错误 指出 卷曲错误 操作在 0 毫秒后超时 0 中的 0 收到的字节数 这是服务器的问题还是我的 GCM 代码的问题 下面是我的 ph
  • “params”和“数组参数”有什么区别以及什么时候应该使用它?

    这两种方法的具体区别是什么 何时使用 params 以及何时使用数组参数 如果能得到答复 我们将不胜感激 passing array to a method class Program static void printarray int
  • Xcode 9、iOS 10 的资产问题

    我在使用 iOS 10 的设备上运行资产和 Xcode 9 beta 时遇到问题 它不会从资产上传任何图像 因此在手机上我看不到任何资产 如果我在故事板中加载图像 它不会在手机上显示任何图像 但它会显示在故事板中 如果我执行以下操作 UII
  • 了解 hibernate @Type 注解

    来自休眠官方文档 http docs jboss org ejb3 app server HibernateAnnotations reference en html single d0e2018 org hibernate annotat
  • 如何将资源添加到现有签名程序集

    鉴于 程序集 例如 SomeLib dll 文本文件 例如 myconfig xml 我需要将文件 myconfig xml 嵌入到现有程序集 SomeLib dll 中 在按 回答 之前请考虑 我知道编译过程中的资源嵌入 csc exe
  • 如何使用php测试服务器是否支持文件上传

    这是我测试 MySQL 的代码 if extension loaded mysqlnd echo mysql supported else echo mysql not supported 如何检查是否允许上传 if ini get fil
  • Flutter iOS 构建错误 - 找不到框架 image_picker

    我已经添加了 v0 8 5 的图像选择器 https pub dev packages image picker我的项目中的库 我已尝试以下方法来解决该问题 Deleted podfile lock Pods folder from iOS
  • 将 FB 开放图操作、对象和聚合从开发应用程序迁移到生产应用程序

    一直在破解 facebook open graph 以将项目发布到用户流 目前我已经使用测试应用程序创建了所有操作 对象 聚合 然而我会希望 a 将其迁移到我们的临时服务器进行测试 b 一旦 a 完成 将其迁移到生产服务器 鉴于每个 FB
  • ngx-leaflet/Angular 2 中具有下拉选择输入的属性绑定映射单击事件

    我有一张带有区域多边形的地图 在地图下方 我有一个下拉选择输入 可以动态读取多边形名称作为选项 当用户单击地图上的区域多边形时 我希望下拉选择输入使用选定的多边形名称 作为 geojson 文件中的 名称 字段存在 进行更新 我认为实现此目