reactjs - 反应原生地图:我无法为我的反应原生地图渲染标记?
问题描述
import React from 'react';
import { StyleSheet, Text, View, Component, Dimensions } from 'react-native';
import { fb } from "./Firebase/data";
import firestore from 'firebase/firestore';
import MapView, { Marker } from 'react-native-maps';
export default class App extends React.Component {
state = {
nodes: []
}
componentDidMount(){
const db = fb.firestore()
.collection('nodes')
.get()
.then( snapshot => {
const nodes = []
snapshot.forEach( doc =>{
const data = doc.data()
nodes.push(data)
});
this.setState({
nodes : nodes
})
});
}
render() {
console.log(this.state.nodes.lat)
return (
<MapView
style={{ ...StyleSheet.absoluteFillObject }}
initialRegion={{
latitude: 28.5450,
longitude: 77.1926,
latitudeDelta: 0.0039922,
longitudeDelta: 0.0039421,
}} >
{this.state.nodes.map((marker, index) => {
<Mapview.Marker
key = {index}
coordinate = {{ latitude : marker.lat},
{ longitude : marker.lon}}
title = { marker.location }
/>
}
)
}
</MapView>
);
}
}
console.disableYellowBox = true;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
mapStyle: {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
},
})
这是我的 react native 项目的父代码。如果需要,我还可以显示导出 fb 代码,但我相信它可以正常工作
有人可以告诉我这里出了什么问题,因为当我不调用 Mapview 时控制台上提供了来自 firebase 的数据,但就在我调用 Mapview 时,控制台显示 Array []。
我只想在地图上渲染标记
解决方案
您没有在地图上返回任何内容:
错了:this.state.nodes.map(()=>{})
,
真的 :this.state.nodes.map(()=>())
真正的 v2:this.state.nodes.map(()=>{ return ... })
最终代码应如下所示:
{this.state.nodes.map((marker, index) => (
<Mapview.Marker
key={index}
coordinate={({ latitude: marker.lat }, { longitude: marker.lon })}
title={marker.location}
/>
))}
推荐阅读
- javascript - 当前浏览器 ITP 的 Cookie 读/写策略和源策略
- android - 作为 Android 手机、平板电脑、iPad、iOS 发送 CURL 请求
- java - 如果正在使用不同的线程,Spring Cloud Stream 可轮询消费者 dlq 和 errorChannel 将不起作用
- python - 有没有办法创建一个不断点击链接的python脚本?
- xml - 删除不符合条件的大 XML 文件的部分 - 使用 bash
- networkx - 将 pytorch 几何数据样本转换为其对应的折线图
- angular - 如何使用 rx-angular 设置嵌套属性状态
- android - 带有谷歌签名的安卓应用发布
- c# - 如何使用包含嵌入的 OCSP 的 iText 签署 pdf
- sql - 如何决定是否拥有外键