如何使用TurfJS获得所有最近的位置?

时间:2019-04-29 14:21:45

标签: javascript reactjs google-maps react-native firebase-realtime-database

我正在做React Native App使用Maps,并与实时DB Firebase一起使用来获取包含纬度,经度等的所有数据。

我得到当前位置[经度和纬度]计算当前位置与我用TurfJs到达的最近位置之间的距离

我的问题是我需要显示数据库中距离我当前位置最近的所有用户。 但是该功能只是使第一个最接近!如何处理?

节点 Node

功能

handleNearby = () => {
const { region, providers } = this.state;
  let points = providers.map(p =>
    turf.point([p.coordinates.latitude, p.coordinates.longitude])
  );
  let collection = turf.featureCollection(points);
  let currentPoint = turf.point([region.longitude, region.latitude]);
  let nearest = turf.nearestPoint(currentPoint, collection);
  let addToMap = [currentPoint, points, nearest];
  console.log(nearest);
  console.log(Math.floor(nearest.properties.distanceToPoint)); // let's say 50Km
};

<MapView
  style={[styles.map, { width: this.state.width }]}
  style={StyleSheet.absoluteFill}
  // onMapReady={() => console.log(this.state.region)}
  showsUserLocation={true}
  region={region}
  loadingEnabled={true}
  // style={StyleSheet.absoluteFill}
  textStyle={{ color: "#bc8b00" }}
  containerStyle={{ backgroundColor: "white", borderColor: "#BC8B00" }}
>
  <Marker
    coordinate={region}
    title="Hello"
    description="description"
    pinColor="navy"
    // onPress={() => alert("Provider Profile")}
    // icon={require('../assets/camera-icon.png')}
    onCalloutPress={() => alert("Provider Profile")}
  />
  //
  {this.state.providers.map((marker, index) => (
    <Marker
      key={index}
      coordinate={marker.coordinates}
      title={marker.username}
    />
  ))}
  // Nearest Point Marker Should Be Here
</MapView>;

0 个答案:

没有答案