react-native geolocation.watchPosition不起作用

时间:2018-01-18 04:58:33

标签: android react-native geolocation react-native-android react-native-maps

constructor(porps) {
  super(porps);
  this.state = {
    initialPosition: {
      latitude: 0,
      longitude: 0,
      latitudeDelta: 0,
      longitudeDelta: 0
    },
    markerPosition: {
      latitude: 0,
      longitude: 0
    }


  };


}


componentDidMount() {
  if (Platform.Version === 23) {
    this.setState({
      version: 23
    });
  } else {
    this.setState({
      version: 0
    });
  }

  navigator.geolocation.getCurrentPosition((position) => {
    var lat = parseFloat(position.coords.latitude);
    var lng = parseFloat(position.coords.longitude);

    var initialRegion = {
      latitude: lat,
      longitude: lng,
      latitudeDelta: latitudeDelta,
      longitudeDelta: longitudeDelta
    };

    var testPos = {
      latitude: lat,
      longitude: lng,
    }

    this.setState({
      initialPosition: initialRegion
    });
    this.setState({
      markerPosition: initialRegion
    });

  }, (error) => alert(JSON.stringify(error)), {
    enableHighAccuracy: true,
    timeout: 20000,
    maximumAge: 1000
  })

  this.watchId = navigator.geolocation.watchPosition((position) => {
    var lat = parseFloat(position.coords.latitude);
    var lng = parseFloat(position.coords.longitude);

    var lastRegion = {
      latitude: lat,
      longitude: lng,
      latitudeDelta: latitudeDelta,
      longitudeDelta: longitudeDelta
    };

    this.setState({
      initialPosition: lastRegion
    });
    this.setState({
      markerPosition: lastRegion
    })
  });

}

navigator.geolocation.getCurrentPosition - 工作正常。 navigator.geolocation.watchPosition - 给我错误。

此代码仅在android上返回错误(在IOS上完美运行)。

ERROR SCREENSHOT

0 个答案:

没有答案