2016-12-13 12 views
3

IOSでは、GPS座標を探しているときに問題はありません。それはうまく動作します。ネイティブAndroidの位置情報要求にタイムアウトが発生する

Android側では、IOSとしては安定していません。この問題は、実デバイスとエミュレータの両方で発生します。場合によっては場所を見つけることができますが、時にはそうではありません。三日を探していたが解決策が見つからなかった。

私のアプリが私の場所を見つけることができないとき、私はGoogleマップアプリを使ってみましたが、それは魅力的です。

IOSとAndroidの両方のコードです。

getCurrentPosition() { 
    navigator.geolocation.getCurrentPosition(
     (position) => { 
      this.setState({ initialPosition: position }); 
      alert(JSON.stringify(position)) 
      var coords = new Coords(); 
      coords.Latitude = position.coords.latitude; 
      coords.Longitude = position.coords.longitude; 
      this.getPharmaciesByCoordinates(coords); 
     }, 
     (error) => alert(error.message), 
     { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } 
    ); 
    this.watchID = navigator.geolocation.watchPosition((position) => { 
     this.setState({ initialPosition: position }); 
    }, 
     (error) => alert(error.message), 
     { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } 
    ); 
} 

あらゆる種類のソリューションを歓迎します。

おかげ

+0

これは私がこれを読んhttps://github.com/facebook/react-native/issues/7495 –

+0

を助けることができ、これに解決策が機能していません。また、あなたに興味を持っていただきありがとうございます –

+0

これに対応する解決策は見つかりましたか?私は同じ問題に直面しています。どこにでも解決策が見つかりませんでした。 – manjs

答えて

1

は私がネイティブLocationManagerを使用する外部のlibを経由して解決策を見つけました。それは、私が望むように、プレイサービスロケーションと発光ロケーションイベントを使用します。

ここにライブラリがあります。 https://bitbucket.org/timhagn/react-native-google-locations#readme

とAndroid用にコンパイルする場合、​​

dependencies { 
    ... 
    compile "com.google.android.gms:play-services:10.0.1" -> which version that you have write down here. 
    ... 
} 

を含めることを忘れないでくださいそして最後にのAndroid SDK ManagerでGoogle Play ServicesGoogle Support Libraryをダウンロードすることを忘れないでくださいあなたのプレイ・サービスのバージョンを見つけることができますに;

<android-sdk-location>/<sdk-version>/extras/google/m2repository/com/google/android/gms/play-services 
3

建物やオフィスの中にいる可能性があり、信号があまり良くない可能性があります。私は多くの構成で試してみましたが、これは私のために最適です。

{ 
    enableHighAccuracy: false, 
    timeout: 5000, 
    maximumAge: 10000 
} 

またはタイムアウトを長くしてみてください。私にとっては、通常の2000msが動作していなかったので、私は "Location request timed out"を取得し続けました。したがって、高精度を無効にし、タイムアウトを増やしてください。

-1

トリックは、あなたが

{ enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 } 

を設定した場合、それが動作する、です。下の完全なコード。

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

class GeolocationExample extends Component { 
constructor(props) { 
super(props); 

this.state = { 
    latitude: null, 
    longitude: null, 
    error: null, 
}; 
} 

componentDidMount() { 
navigator.geolocation.getCurrentPosition(
    (position) => { 
    this.setState({ 
     latitude: position.coords.latitude, 
     longitude: position.coords.longitude, 
     error: null, 
     }); 
     }, 
    (error) => this.setState({ error: error.message }), 
    { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }, 
);} 

render() { 
return (
    <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}> 
    <Text>Latitude: {this.state.latitude}</Text> 
    <Text>Longitude: {this.state.longitude}</Text> 
    {this.state.error ? <Text>Error: {this.state.error}</Text> : null} 
    </View> 
); 
} 
} 

export default GeolocationExample; 
+0

キャッシュからのものではなく、現在の正しい場所がどのようになると思いますか? –

関連する問題