如何在 React Native MapView 中获取用户位置周围的标记?
我想获取最接近用户位置的前 5 个标记(按顺序)并将其放入 useState 对象中。我怎样才能做到这一点?
我的地图屏幕负载流程:
- 加载地图
- 获取用户位置(存储在位置 useState 中)
- HTTP 获取标记位置请求(每个标记获得纬度和经纬度)
- 获取用户周围的标记(如果同意共享位置)
- 显示屏幕
const fetchClosestRendelok = async () => {
//Get markers in an area here and add to a json
}
const fetchRendeloData = async () => {
const data = await fetchRendelok()
setRendelok(data)
}
useEffect(() => {
fetchRendeloData()
fetchClosestRendelok()
}, []);
return (
<View style={styles.container}>
<MapView style={styles.map}
provider={PROVIDER_GOOGLE}
region={mapRegion}
showsUserLocation={true}
onPress={() => setSelectedMarkerIndex("")}
>
{
rendelok.map((rendelok) => {
return (
<Marker
key={rendelok.id}
coordinate={{ latitude:parseFloat(rendelok.lat), longitude:parseFloat(rendelok.ltd) }}
onPress={() => markerClick(rendelok)}
tracksViewChanges={false}
>
<View>
<MaterialCommunityIcons name="hospital-marker" size={selectedMarkerIndex === rendelok.id ? 80 : 30} color={selectedMarkerIndex === rendelok.id ? 'red' : color_theme_light.iconColor}/>
</View>
</Marker >
)
})
}
</MapView>
</View>
);
}
I want to fetch top 5 markers that are closest to the users location (in order) and put that into a useState object. How can I achieve that?
My map screen loadflow:
- Load map
- Get user location (stored in location useState)
- HTTP Get request for markers location (each marker got lat and ltd)
- Fetch markers around user (if agreed to share location)
- Display screen
const fetchClosestRendelok = async () => {
//Get markers in an area here and add to a json
}
const fetchRendeloData = async () => {
const data = await fetchRendelok()
setRendelok(data)
}
useEffect(() => {
fetchRendeloData()
fetchClosestRendelok()
}, []);
return (
<View style={styles.container}>
<MapView style={styles.map}
provider={PROVIDER_GOOGLE}
region={mapRegion}
showsUserLocation={true}
onPress={() => setSelectedMarkerIndex("")}
>
{
rendelok.map((rendelok) => {
return (
<Marker
key={rendelok.id}
coordinate={{ latitude:parseFloat(rendelok.lat), longitude:parseFloat(rendelok.ltd) }}
onPress={() => markerClick(rendelok)}
tracksViewChanges={false}
>
<View>
<MaterialCommunityIcons name="hospital-marker" size={selectedMarkerIndex === rendelok.id ? 80 : 30} color={selectedMarkerIndex === rendelok.id ? 'red' : color_theme_light.iconColor}/>
</View>
</Marker >
)
})
}
</MapView>
</View>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,这是我的解决方案,我使用 libary 按距离对标记进行排序 https://github.com/manuelbieh /geolib,然后我只是将坐标与原始数组进行比较。
In the end this was my solution, I used libary to order my markers by distance https://github.com/manuelbieh/geolib, then I just compared the coords with my original array.