简体   繁体   English

Angular2-Leafletjs:为L.geoJSON()定义样式.addTo(myMap)

[英]Angular2-Leafletjs: Defining style for L.geoJSON().addTo(myMap)

In am using Leafletjs in Angular2 , http.get to invoke JSON from a URL, but I can't define style for the features that are added to the map because of predefining the geoJSON layer: 在使用Angular2中的Leafletjs时http.get从URL调用JSON,但由于预定义了geoJSON层,我无法为添加到地图的要素定义样式:

  // Add an empty layer to the map
  var geoJsonLayer1 = L.geoJSON().addTo(myMap);

  // Retrieve the geojson file
  http.get(myJsonURL)
  .map((response: Response) => {
      geoJsonLayer1.addData(response.json());
  }).subscribe();

The geoJSON layer doesn't have any style and it looks as below: geoJSON图层没有任何样式,如下所示:

在此输入图像描述

The reason that I am predefining the layer is, I am lazy-loading the JSON file. 我正在预定义图层的原因是,我懒得加载JSON文件。 In the regular way, we can define the style as below: 在常规方式中,我们可以定义如下样式:

L.geoJSON(myLines, {
    style: myStyle
}).addTo(map);

Now my question is, how to define the style for a predefined layer? 现在我的问题是,如何定义预定义图层的样式?

Just predefine the layer without data… 只需预定义没有数据的图层......

var geoJsonLayer1 = L.geoJSON(null, {
  style: myStyle
}).addTo(map);

// Later on…
geoJsonLayer1.addData(response.json());

BTW your screenshot looks to show markers (for "Point" type features) with missing icon image. 顺便说一句,您的屏幕截图看起来显示缺少图标图像的标记( "Point"类型功能)。 In that case, instead of style option for vector shapes (like polygons, line strings, etc.), you should use the pointToLayer option. 在这种情况下,您应该使用pointToLayer选项,而不是矢量形状的style选项(如多边形,线条等)。

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

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