简体   繁体   English

用于GeoJSON的自定义Leaflet Renderer

[英]Custom Leaflet Renderer for GeoJSON

I want to render GeoJSON differently than provided by Leaflet by default. 默认情况下,我想以不同于Leaflet提供的方式呈现GeoJSON。 Unfortunately changing the style() function will not suffice as I need greater control over the vector graphics being created. 不幸的是,更改style()函数不足以满足我的需要,因为我需要对创建的矢量图形进行更大的控制。

What is the idiomatic way of doing this? 惯用的方式是什么? I reckon I can reuse lots of parts of the default SVG renderer found in src/layers/vector/SVG.js . 我认为我可以重复使用src/layers/vector/SVG.js中的默认SVG渲染器的许多部分。 I would however like to only replace it for one GeoJSON layer / one FeatureGroup. 但是,我只想将其替换为一个GeoJSON图层/一个FeatureGroup。 As none of these inherit from Path , this does not seem possible. 由于这些都不是继承自Path ,因此这似乎是不可能的。

I tried extending the SVG class in a very basic way: 我尝试以一种非常基本的方式扩展SVG类:

import {SVG} from 'leaflet/src/layer/vector/SVG'

export default class CustomSVG extends SVG {
  _updatePoly (layer, closed) {
    console.log('Custom renderer in action')
    super._updatePoly(layer, closed)
  }
}

and then set it as the renderer while initializing Leaflet, but this fails with a TypeError: max2 is undefined (it is an unhandled promise rejection which stems from me using react-leaflet ). 然后在初始化Leaflet时将其设置为渲染器,但这失败,并出现TypeError: max2 is undefined (这是未处理的Promise拒绝,源于我使用react-leaflet )。

What is it I'm missing? 我想念的是什么?

I read the documentation again and I seem to have missed this bit about the GeoJSON options : 我再次阅读了文档,似乎错过了有关GeoJSON选项的这一点

style : A Function defining the Path options for styling GeoJSON lines and polygons, called internally when data is added. style :一个函数,用于定义用于设置GeoJSON线和多边形的Path选项 ,在添加数据时在内部调用。 The default value is to not override any defaults: 默认值是不覆盖任何默认值:

 function (geoJsonFeature) { return {} } 

The path options on the other hand take a renderer, which I can use to override the rendering of this particular GeoJSON layer only. 另一方面,路径选项带有一个渲染器,我可以使用它来覆盖此特定GeoJSON图层的渲染。 Hope this helps anybody else! 希望这对其他人有帮助!

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

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