简体   繁体   English

react-mapbox-gl中的样式标记/功能?

[英]Stylable marker/feature in react-mapbox-gl?

Im using https://github.com/alex3165/react-mapbox-gl 我正在使用https://github.com/alex3165/react-mapbox-gl

My question is how to style the marker or feature component? 我的问题是如何设置标记或要素组件的样式?

Feature does not accept children nor style prop. 功能接受儿童,也不接受样式道具。

Marker accepts style and children, however it doesnt render anything passed as props and even if I style it with, eg {{ background: 'blue' }} it has no any effects on the style. 标记接受样式和子元素,但是它不会渲染任何通过道具传递的内容,即使我使用{{ background: 'blue' }}对其进行样式设置,它也不会对样式产生任何影响。

Have I missed something? 我错过了什么吗? Thanks 谢谢

Marker and Feature are two different components which work in different ways but can both achieve what you are trying to do. 标记和功能是两个不同的组件,它们以不同的方式工作,但是都可以实现您想要的工作。 Let's start with Markers. 让我们从标记开始。

Marker Styling 标记样式

You will note that in the react-mapbox-gl documentation that the style attribute will only effect the marker's container, not the marker itself. 您会注意到,在react-mapbox-gl文档中, style属性只会影响标记的容器,而不会影响标记本身。 If you want to change the style of a Marker you should pass in your own marker as a child to the Marker component. 如果要更改标记的样式,则应将自己的标记作为子代传递给标记组件。 Failing to pass in a child will result in the default light blue, droplet shaped marker being used. 未能通过孩子将导致使用默认的浅蓝色,水滴形标记。 Note that this child you pass in as the marker could be a custom svg or even an html component that is styled with CSS . 请注意,您作为标记传递的孩子可能是自定义svg甚至是使用CSS样式html组件

<Marker
  coordinates={[-0.2416815, 51.5285582]}
  anchor="bottom">
  <img src={linkToMyCustomMarker}/>
</Marker>

or... 要么...

<Marker
  coordinates={[-0.2416815, 51.5285582]}
  anchor="bottom">
  <div class="mapMarkerStyle"></div>
</Marker>

Here's a Code Sandbox showing this in action: https://codesandbox.io/s/my2p15knj8 这是一个代码沙箱,它正在起作用: https : //codesandbox.io/s/my2p15knj8

Layer Styling 图层样式

In order to style Features you will first need to use the Layer component, as mentioned in the documentation for Feature . 为了设置要素样式,您首先需要使用Layer组件,如Feature文档中所述。 In the documentation for the Layer component you can see that you must set up your layer first and then pass in the Feature component(s) as a child for every location on the map that you would like to render this Layer. 在“ 图层”组件文档中,您可以看到必须首先设置图层,然后将要素组件作为子级传递给要渲染该图层的地图上每个位置。 Like so: 像这样:

<Layer type="circle" id="marker" paint={{
  'circle-color': "#ff5200",
  'circle-stroke-width': 1,
  'circle-stroke-color': '#fff',
  'circle-stroke-opacity': 1
 }}>
  <Feature coordinates={[-0.132,51.518]}/>
  <Feature coordinates={[-0.465,51.258]}/>
</Layer>

Here is a Code Sandbox showing the above in action: https://codesandbox.io/s/l42n3np7xm 这是一个代码沙箱,上面显示了上述操作: https : //codesandbox.io/s/l42n3np7xm

In order to change the look and feel of the Layer that is displayed you can play around with the layout property on the Layer component. 为了更改显示的图层的外观,您可以在图层组件上使用layout属性。 All of the settings that you can change can be found in the Mapbox Style Definition . 您可以更改的所有设置都可以在Mapbox样式定义中找到

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

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