0

更改设备的位置时,不会在地图上实时更新。即使在更改坐标时,屏幕也会不停地闪烁,试图定位我的位置以遵循 react-native-maps-directions 的路线。我的标记不会停止移动位置,尽管没有移动位置,它也不会保持静止。每次移动标记时,它都会停止显示地图视图方向。

useEffect(() => {
  (async () => {
    const resultPermissions = await Permissions.askAsync(Permissions.LOCATION);
    const statusPermissions = resultPermissions.permissions.location.status;

    if (statusPermissions !== "granted") {
      toastRef.current.show(
        "Tienes que aceptar los permisos de localizacion",
        3000
      );
      setpermiso(false);
    } else {
      try {
        let loc = await Location.watchPositionAsync(
          {
            accuracy: Location.Accuracy.High,
            // timeInterval: 300,
            distanceInterval: 0,
          },
          (loc) => {
            actualizarUbicacion(loc.coords.latitude, loc.coords.longitude);
            setLocation({
              latitude: loc.coords.latitude,
              longitude: loc.coords.longitude,
              latitudeDelta: 0.001,
              longitudeDelta: 0.001,
            });
            console.log(loc.coords.latitude, loc.coords.longitude);
          }
        );

        setpermiso(true);
      } catch (e) {
        console.log(e);
      }
    }
  })();
}, []);

每次位置更改时,firebase 中的信息都会更新。

<MapView
  ref={mapView}
  style={styles.mapStyle}
  initialRegion={location}
  showsPointsOfInterest={false}
>
  <Marker
    coordinate={location}
    rotation={angle}
    flat={true}
    anchor={{ x: 0.5, y: 0.5 }}
  >
    <Image
      style={{
        width: 40,
        height: 40,
        resizeMode: "contain",
      }}
      source={car}
    />
  </Marker>

  <Marker
    coordinate={{
      latitude: latitudeVendedor,
      longitude: longitudeVendedor,
    }}
  >
    <Image
      style={{
        width: 40,
        height: 40,
        resizeMode: "contain",
      }}
      source={pin}
    />
  </Marker>
  <MapViewDirections
    origin={location}
    destination={{
      latitude: latitudeVendedor,
      longitude: longitudeVendedor,
    }}
    precision={"high"}
    apikey={GOOGLE_MAPS_APIKEY}
    strokeWidth={4}
    onReady={(result) => {
      setdistancia(result.distance);
      settiempo(result.duration);

      // Reposition the car
      let nextLoc = {
        latitude: result.coordinates[0]["latitude"],
        longitude: result.coordinates[0]["longitude"],
        latitudeDelta: 0.001,
        longitudeDelta: 0.001,
      };

      if (result.coordinates.length >= 2) {
        let angle = calculateAngle(result.coordinates);
        setangle(angle);
      }
      setLocation(nextLoc);
    }}
  />
</MapView>;

我究竟做错了什么?

4

0 回答 0