如何动态更新MapboxGL.ShapeSource?

问题描述 投票:2回答:1

使用react-native-mapbox-gl/maps,当将SymbolLayer动态添加到ShapeSource时,似乎没有显示,或者ShapeSource未更新。

这里是要重现的示例:基于CustomIcon example,我将代码替换为下面的代码。要进行复制,只需复制CustomIcon,然后复制粘贴该代码以代替execute the examples示例中的现有代码。

CustomIcon.js

我们可以看到,单击地图会更改状态,添加要素,但不会在地图上显示该要素。如何使import React from 'react'; import { View, Text } from 'react-native'; import MapboxGL from '@react-native-mapbox-gl/maps'; import sheet from '../styles/sheet'; import BaseExamplePropTypes from './common/BaseExamplePropTypes'; import Page from './common/Page'; import Bubble from './common/Bubble'; const styles = { icon: { iconAllowOverlap: true, }, view: { width: 60, height: 60, borderColor: 'black', borderWidth: 1, alignItems: 'center', justifyContent: 'center' }, text: { fontSize: 50 } }; const customIcons = ['😀', '🤣', '😋', '😢', '😬'] class CustomIcon extends React.Component { constructor(props) { super(props); this.state = { featureCollection: { type: 'FeatureCollection', features: [{ type: 'Feature', geometry: { coordinates: [-73.970895, 40.723279], type: 'Point' }, id: 1, properties: { customIcon: customIcons[0] } }] }, }; this.onPress = this.onPress.bind(this); this.onSourceLayerPress = this.onSourceLayerPress.bind(this); } onPress(e) { const feature = { type: 'Feature', geometry: e.geometry, id: Date.now(), properties: { customIcon: customIcons[this.state.featureCollection.features.length] } }; this.setState(({ featureCollection }) => ({ featureCollection: { type: 'FeatureCollection', features: [ ...featureCollection.features, feature ] } })); } onSourceLayerPress(e) { const feature = e.nativeEvent.payload; console.log('You pressed a layer here is your feature', feature); // eslint-disable-line } render() { return ( <Page {...this.props}> <MapboxGL.MapView ref={c => (this._map = c)} onPress={this.onPress} style={sheet.matchParent} > <MapboxGL.Camera zoomLevel={9} centerCoordinate={[-73.970895, 40.723279]} /> <MapboxGL.ShapeSource id="symbolLocationSource" hitbox={{width: 20, height: 20}} onPress={this.onSourceLayerPress} shape={this.state.featureCollection} > {this.state.featureCollection.features.map((feature, ind) => ( <MapboxGL.SymbolLayer id={"symbolLocationSymbols" + feature.id} key={feature.id} filter={['==', 'customIcon', customIcons[ind]]} minZoomLevel={1} style={styles.icon} > <View style={styles.view}> <Text style={styles.text}> {feature.properties.customIcon} </Text> </View> </MapboxGL.SymbolLayer> ))} </MapboxGL.ShapeSource> </MapboxGL.MapView> <Bubble> <Text>Tap to add an icon</Text> </Bubble> </Page> ); } } export default CustomIcon; 动态更新?

reactjs react-native mapbox mapbox-gl-js mapbox-gl
1个回答
0
投票

关于该主题的整个讨论都在这里:ShapeSource

为了简短起见,我想将动态SVG用作https://github.com/react-native-mapbox-gl/maps/issues/248(以便例如可以更改颜色),但这是不可能的:给SymbolLayer任何子组件都不是正确的方法。

由于我们可以动态更新SymbolLayer,因此我们需要与ImagesShapeSource并行使用SymbolLayer。>

这里是一个代码示例:

Images
© www.soinside.com 2019 - 2024. All rights reserved.