简体   繁体   English

使用ngx-leaflet添加带有角度的geoJson图层

[英]Add geoJson layer with angular with ngx-leaflet

I've set up my application using angular5 with ngx-leaflet ( https://github.com/Asymmetrik/ngx-leaflet ). 我已经使用angular5和ngx-leaflet( https://github.com/Asymmetrik/ngx-leaflet )设置了我的应用程序。

I start with a blank map. 我从一张空白的地图开始。 When a user clicks a button, markers appear on the map. 用户单击按钮时,标记将显示在地图上。 When a user selects a marker, that marker is identified to a country and that country is linked with other countries based on some other (non-important) data. 当用户选择标记时,该标记会被识别到一个国家,并且该国家会基于其他(非重要)数据与其他国家/地区链接。

<div id="map-sidebar">
    <div class="map-buttons-bar">
        <button class="btn btn-primary" (click)="addLayer()">Set Marker</button>
    </div>
</div> 

In my .ts file, I have 在我的.ts文件中,

addLayer(){
    var n_marker = marker([38.9072, -77.0369], {icon: this.anomalyIcon});
    n_marker.on('click', (e)=>{             
        this.mapToPartnerCountries();
    })
    this.lLayers.push(n_marker);
  }

mapToPartnerCountries(){

    var n_marker = marker([39.9072, -78.0369], {icon: this.anomalyIcon});
    this.lLayers.push(n_marker); 
    //var newLayer = geoJSON(geo as any);
    //this.lLayers.push(newLayer);
  }

My issue is the new layer does not appear when the mapToPartnerCountries() method is run. 我的问题是,运行mapToPartnerCountries()方法时不会出现新图层。 But if I click the button again, it appears. 但是,如果我再次单击该按钮,它将出现。 GeoJson does the same thing as well. GeoJson也做同样的事情。 Am I doing this the correct way? 我这样做正确吗?

Also the this.lLayers field is used in the map element... 还在地图元素中使用this.lLayers字段...

<div class="map-container" 
            leaflet 
            [leafletOptions]="mapOptions" 
            [leafletLayersControl]="layersControl"
            [leafletLayers]="lLayers"
        >

Angular uses zones to determine what events do and do not trigger change detection. Angular使用区域来确定发生和不触发更改检测的事件。 Any event that is propagated from a template (eg, the (click) event) will trigger change detection unless you specifically configure it not to. 从模板传播的任何事件(例如,(单击)事件)都将触发更改检测,除非您明确配置为不这样做。 That is why when you click again, the changes are applied. 因此,当您再次单击时,将应用更改。

Leaflet events are intentionally set up to happen outside of the Angular zone (for various reasons, but mostly performance related). 故意将传单事件设置为在Angular区域之外发生(出于各种原因,但主要与性能相关)。 You can read more here . 您可以在这里阅读更多内容。 Angular won't detect any changes made to component properties if the changes are made outside of the Angular zone. 如果在Angular区域之外进行更改,Angular将不会检测到对组件属性所做的任何更改。

You are modifying this.lLayers inside of a callback on a Marker click event, which is likely occurring outside of Angular's zone. 您正在Marker click事件的回调内部修改this.lLayers ,这很可能发生在Angular区域之外。 To get Angular to "see" this modification, you will need to perform it inside of Angular's zone using NgZone.run() . 要使Angular“看到”此修改,您需要使用NgZone.run()在Angular的区域内执行它。

For example: 例如:

constructor(private zone: NgZone) {}

addLayer(){
   var n_marker = marker([38.9072, -77.0369], {icon: this.anomalyIcon});
   n_marker.on('click', (e) => {

      this.zone.run(() => {         
         this.mapToPartnerCountries();
      });

   })
   this.lLayers.push(n_marker);
}

That code will ensure that this.mapToPartnerCountries(); 该代码将确保this.mapToPartnerCountries(); is run inside of Angular's zone so that change detection will be performed. 在Angular区域内运行,以便执行更改检测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM