我成功地从我的 API 中检索了一组功能,当我将它们输出到例如控制台时,它会是这样的:
{type: "feature" geometry: {type: point, coordinates: array(2)}
properties: {extra properties}}
这些特性中有多个是从我的 API 中检索出来的,并存储在 state 中。目前检索到的最大特征量在 0 到 50 个特征之间。所以我猜这不是需要渲染太多功能的问题。
在下面链接的项目 GitHub 页面上找到了一个示例,该示例执行了类似的操作,但他们使用的是 url 而不是我正在使用的形状属性。哪个应该能够根据文档进行尝试。
示例链接: https ://github.com/react-native-mapbox-gl/maps/blob/master/example/src/examples/EarthQuakes.js
链接到文档: https ://github.com/react-native-mapbox-gl/maps/blob/master/docs/ShapeSource.md
我还尝试了在这篇 SO 帖子中找到的几种解决方案: 如何将来自服务器的普通地理 json 转换为 geoJson?
目前我的渲染方法看起来像下面提供的那样:
render() {
return (
<View style={styles.container}>
<MapboxGL.MapView
showUserLocation={true}
rotateEnabled={false}
style={styles.map}
ref={map => { this.map = map; }}
styleURL={MapboxGL.StyleURL.Street}
onRegionDidChange={this.onRegionDidChange}
onDidFinishLoadingMap={this.onDidFinishLoadingMap}>
<MapboxGL.UserLocation
visible={true}/>
<MapboxGL.ShapeSource
id='routeSource'
shape={{type: "FeatureCollection", features:
this.state.routes}}
type='geojson'>
<MapboxGL.CircleLayer
id="singlePoint"
style={layerStyle.singlePoint}
/>
</MapboxGL.ShapeSource>
<MapboxGL.Camera
ref={camera => {this.camera = camera;}}
zoomLevel={8}/>
</MapboxGL.MapView>
</View>
);
所有代码在 android 和 iOS 上都可以正常编译,控制台或模拟器中都没有错误。预期结果是使用添加到状态的特征在地图上渲染圆圈