firebase - How can I get my Firebase Data into React-Native Map Marker instead of a Flatlist
问题描述
Im trying to get all my Firebase Docs/Posts into React-Native-Maps Markers and show them on a map(like AirBnB for example) but couldn't found out the wright way till now..
all my functions are working fine with a flatlist as you can see in the screenshot but how can I get the data into the markers? Ive tried to get the data + renderItem function from the Flatlist tag and put it into the MapView tag but it didn't work because of the open brace/funtion.
Im fairly new in JS/RN.. so im maybe not really able to understand all of the technical terminology. I would be very thankful if someone has a solution for that.. im struggling for several weeks with this issue.
Fire.js function:
export async function getMarker(markerRetreived) {
var marker = [];
var snapshot = await firebase.firestore()
.collection('Events')
.orderBy('createdAt')
.get()
snapshot.forEach((doc) => {
const markerItem = doc.data();
markerItem.id = doc.id;
marker.push(markerItem);
});
markerRetreived(marker);
}
Working Flatlist (map.js):
class FocusOnMarkers extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
marker: []
}
}
onMarkerReceived = (marker) => {
this.setState(prevState => ({
marker: prevState.marker = marker
}));
}
componentDidMount() {
getMarker(this.onMarkerReceived);
}
render() {
return (
<SafeAreaView style={styles.container}>
<FlatList style={styles.container}
data={this.state.marker}
renderItem={({ item }) => {
return (
<ListItem
containerStyle={styles.listItem}
title={`lat: ${item.geopoint.latitude}`}
subtitle={`lng: ${item.geopoint.longitude}`}
titleStyle={styles.titleStyle}
subtitleStyle={styles.subtitleStyle}
leftAvatar={{
size: 'large',
rounded: false,
source: item.image && { uri: item.image }
}}
/>
)
}
}
/>
</SafeAreaView>
)
}
}
React-Native-Map:
render() {
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<MapView
provider={PROVIDER_GOOGLE}
mapType='hybrid'
showsUserLocation style={{flex: 1}}>
<MapView.Marker
data={this.state.marker}
coordinate={{latitude: this.state.geopoint.latitude,
longitude:this.state.geopoint.longitude}}
title={("Test")}
description={("Test")}
/>
</MapView>
</View>
</SafeAreaView>
);
}
}
解决方案
我只想让你现在我自己解决了这个问题。
只是在我的 map.js 中更改了一行代码 =>
render() {
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<MapView
provider={PROVIDER_GOOGLE}
mapType='hybrid'
showsUserLocation style={{flex: 1}}>
{this.state.marker.map(item => (
<MapView.Marker
coordinate={{latitude: item.geopoint.latitude,
longitude: item.geopoint.longitude}}
title={("Test")}
description={("Test")}
/>
)}
</MapView>
</View>
</SafeAreaView>
);
}
}
推荐阅读
- python - 为什么我会收到此错误?请需要帮助
- go - 阅读来自 kafka 的最新消息-segmentio/kafka-go
- c# - 如何使用 C# WEBAPI(基于令牌).netcore 进行身份验证和授权 Azure AD 应用程序
- javascript - 替换不可靠字符串 (OCR) 中的已知文本
- r - 应用一个函数来在 ggplots 列表上创建牛图,每次有 4 个图
- c - 在 C 中交换两个字符串
- c - 单链表输出不希望的零
- javascript - 如何在具有 document.write 的 DIV 元素中异步加载 js 内容?
- python - AttributeError:模块“tensorflow”没有属性“random_shuffle”
- c++ - 如何在 Windows Composer 中集成 Qt 无框窗口?(系统快捷方式不起作用)