Xamarin.Formsで作業している場合、ナゲットパッケージXam.Plugin.Geolocatorを使用してみませんか?私はそれを使用しています。私は自分のPCLに自分のロケーションコードを入れました。クロスプラットフォームなので、繰り返しはありません。あなたは本当にAndroidの場所が必要な場合は、このスニペットを取って、あなたのアンドロイドプロジェクトにまっすぐに置くことができます。
return await Plugin.Geolocator.CrossGeolocator.Current.GetPositionAsync();
:必要であれば、これはすべて1行に集光することができる
public class LocationService : ILocationService
{
readonly ILogger _logger;
public LocationService(ILogger logger)
{
_logger = logger;
}
public async Task<IPosition> GetPosition()
{
var locator = Plugin.Geolocator.CrossGeolocator.Current;
//1609 meters in a mile. Half a mile accuracy should be good
//since we are rounding to whole numbers
locator.DesiredAccuracy = 800;
try
{
var position = await locator.GetPositionAsync(5000);
return position.ToPosition();
}
catch (Exception ex)
{
_logger.Log(ex);
return null;
}
}
}
(私は、それ故にラッパークラス及び例のインターフェイスをその影響を最小限に抑えることができるので、私は、通常、プラグインをラップ)
EDIT: プラグインには、IsGeolocationEnabled
とIsGeolocationAvailable
のようなジオロケーションのステータスを判断する方法もあります。 オープンソースなので、implementation on Androidを見ることができます。
エミュレータまたはリアルデバイスを使用していますか? –
エミュレータとリアルデバイスの両方でテストしました。しかし同じ結果が本当に得られる – anand