如何在 React Native MapView 中获取用户位置周围的标记?

发布于 2025-01-12 13:51:36 字数 1716 浏览 2 评论 0原文

我想获取最接近用户位置的前 5 个标记(按顺序)并将其放入 useState 对象中。我怎样才能做到这一点?

我的地图屏幕负载流程:

  1. 加载地图
  2. 获取用户位置(存储在位置 useState 中)
  3. HTTP 获取标记位置请求(每个标记获得纬度和经纬度)
  4. 获取用户周围的标记(如果同意共享位置)
  5. 显示屏幕

在此处输入图像描述

    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:

  1. Load map
  2. Get user location (stored in location useState)
  3. HTTP Get request for markers location (each marker got lat and ltd)
  4. Fetch markers around user (if agreed to share location)
  5. Display screen

enter image description here

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2025-01-19 13:51:36

最后,这是我的解决方案,我使用 libary 按距离对标记进行排序 https://github.com/manuelbieh /geolib,然后我只是将坐标与原始数组进行比较。

  try {
    let coordsArray = []
    rendelok.map((rendelok) => coordsArray.push({latitude:rendelok.lat, longitude:rendelok.ltd}))
    let closestRendelok = orderByDistance(userPosition.coords, coordsArray).slice(0,5);
    var result = rendelok.filter(function (o1) {
      return closestRendelok.some(function (o2) {
          return o1.lat === o2.latitude;
     });
    });
    setClosestRendelok(result)
  } catch (error) {
    console.log(error)
  }

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.

  try {
    let coordsArray = []
    rendelok.map((rendelok) => coordsArray.push({latitude:rendelok.lat, longitude:rendelok.ltd}))
    let closestRendelok = orderByDistance(userPosition.coords, coordsArray).slice(0,5);
    var result = rendelok.filter(function (o1) {
      return closestRendelok.some(function (o2) {
          return o1.lat === o2.latitude;
     });
    });
    setClosestRendelok(result)
  } catch (error) {
    console.log(error)
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文