反应本机地图

时间:2020-09-14 09:23:05

标签: reactjs react-native

我正在忙于开发基于位置的应用程序并利用expo,有没有人知道如何在地图上跟踪用户位置,并告诉他们是否到达了类似于GPS的expo项目上的特定目的地。

      <MapViewDirections
      origin={userContext.userLocation}
      destination={userContext.userLocation}
      apikey={googleMapApi}
      mode='DRIVING'
      language='en's
      strokeWidth={8}
      strokeColor="#2A9D8F"
      optimizeWaypoints={true}
      resetOnChange={false}
      precision={"low"}
  
    />

1 个答案:

答案 0 :(得分:0)

如果您正在使用expo,则应使用expo-location api中的watchPositionAsync https://docs.expo.io/versions/latest/sdk/location/#locationwatchpositionasyncoptions-callback

您应该在此处跟踪更改,然后将坐标放入地图中。

用法


async componentWillMount() {

    const { status } = await Permissions.askAsync(Permissions.LOCATION);

    if (status === 'granted') {
      this._getLocationAsync();
    } else {
      this.setState({ error: 'Locations services needed' });
    }
  }

  componentWillUnmount() {
     this.location.remove(callback...);
  }

  _getLocationAsync = async () => {
      this.location = await Location.watchPositionAsync(
          {
              enableHighAccuracy: true,
              distanceInterval: 1,
              timeInterval: 10000
          },
          newLocation => {
              let coords = newLocation.coords;
          // this.props.getMyLocation sets my reducer state my_location
          this.props.getMyLocation({
              latitude: parseFloat(coords.latitude),
              longitude: parseFloat(coords.longitude)
          });
        },
        error => console.log(error)
      );
      return location;
  };