2017-11-14 8 views
0

私はGps座標を一度オフに照会するアプリを書いています。私の問題は、アプリケーションがGPSサービスをリリースしないことがあることです。 GPSアイコンは画面上に永遠に残っています。考えられる原因は何でしょうか?以下は私のコードです:Xamarin GPSサービスを解放する方法

string latitude = string.Empty; 
string longitude = string.Empty; 
string Area = string.Empty; 
Geocoder geoCoder = null; 
IGeolocator locator = null; 

try 
{ 
    //get gps 
    string myGps = string.Empty; 
    locator = CrossGeolocator.Current; 
    locator.DesiredAccuracy = 50; 

    TimeSpan sp = new TimeSpan(10000); 

    var position = await locator.GetPositionAsync(sp); 

    latitude = position.Latitude.ToString(); 
    longitude = position.Longitude.ToString(); 

    geoCoder = new Geocoder(this.ApplicationContext); 

    var addresses = geoCoder.GetFromLocation(position.Latitude, position.Longitude, 1); 

    if (addresses != null) 
         Area = addresses[0].CountryName + ", " + addresses[0].Locality + ", " + addresses[0].SubLocality + ", " + addresses[0].FeatureName; 

        successful = true; 
} 
catch (Exception ex) 
{ 
} 

答えて

0

おそらく、あなたのGPSが有効になっている場合は、まず試してみてください。

はそれを試してみてください。

 if (CrossGeolocator.Current.IsGeolocationEnabled) 
      GeoLoc = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(10)); 

、あるいはそのような何か:

 if (CrossGeolocator.IsSupported && CrossGeolocator.Current.IsGeolocationAvailable && CrossGeolocator.Current.IsGeolocationEnabled) 
      GeoLoc = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(10)); 
+0

私はあなたのソリューションを試してみました。私はまだこの問題を経験しています。 –

+0

さて、タイムアウトを10秒にして、すべてのコードをtry catchブロックに入れようとしましたか? 何か問題が発生した場合は、例外を取得する必要がありますし、多分それはhaopenを理解するのに役立ちます。 – NourB

+0

はい、私はtry catchに自分のコードを入れました。私は何の誤りもありません。どこでタイムアウトを設定できますか?どのオブジェクトですか? –

関連する問題