2017-06-02 21 views
2

私は奇妙な動作があり、解決策を見つけることができません。私のReact Nativeアプリケーションでは、navigator.geolocationを使用して現在の位置を判断しています。私のアプリケーションはFacebookのジオロケーションコードの例とほとんど同じです。navigator.geolocation.watchPositionがiosデバイスで正常に動作しない

私のアプリケーションは完璧に動作しますが、私のiPhoneに展開すると、(getCurrentPositionとwatchPositionによる)戻り位置が正しくありません。スピードと見出しは-1ですが、精度は65です。経度と緯度は有効ですが、実際の位置ではありません(別の国にあります)。 iphoneにデバッグやリリースでアプリケーションをデプロイするかどうかは関係ありません。常に同じように反応します。

しかし、TomTomを例にしてこのアプリケーションをバックグラウンドで起動し、GeolocationExampleアプリケーションを起動すると、すべてが正常に動作します。

私のアプリケーションは次のように私は作成している:

react-native init GeolocationExample 

をそして私は、このコードでファイルindex.ios.jsを置き換えます。

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View 
} from 'react-native'; 

export default class GeolocationExample extends React.Component { 
    state = { 
    initialPosition: 'unknown', 
    lastPosition: 'unknown', 
    }; 

    watchID: ?number = null; 

    componentDidMount() { 
    navigator.geolocation.getCurrentPosition(
     (position) => { 
     var initialPosition = JSON.stringify(position); 
     this.setState({initialPosition}); 
     }, 
     (error) => alert(JSON.stringify(error)), 
     {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000} 
    ); 
    this.watchID = navigator.geolocation.watchPosition((position) => { 
     var lastPosition = JSON.stringify(position); 
     this.setState({lastPosition}); 
    }); 
    } 

    componentWillUnmount() { 
    navigator.geolocation.clearWatch(this.watchID); 
    } 

    render() { 
    return (
     <View style={styles.main}> 
     <Text> 
      <Text style={styles.title}>Initial position: </Text> 
      {this.state.initialPosition} 
     </Text> 
     <Text> 
      <Text style={styles.title}>Current position: </Text> 
      {this.state.lastPosition} 
     </Text> 
     </View> 
    ); 
    } 
} 

var styles = StyleSheet.create({ 
    main: { 
    margin: 30 
    }, 
    title: { 
    fontWeight: '500', 
    }, 
}); 

AppRegistry.registerComponent('GeolocationExample',() => GeolocationExample); 
+0

@Tim訂正ありがとう – mawi

答えて

0

(1オプション)パラメータを追加{enableHighAccuracy:真、タイムアウト:20000、maximumAgeという:1000、精度:10 // 10メートルではない文書に記載されている} (2オプション)設定線28 の#define RCT_DEFAULT_LOCATION_ACCURACY kCLLocationAccuracyBest //デフォルトはkCLLocationAccuracyHundredMeters

関連する問題