Skip to content

Instantly share code, notes, and snippets.

@3t14
Created January 27, 2017 23:48
Show Gist options
  • Save 3t14/d2375634fdbe03001b69374e5a595320 to your computer and use it in GitHub Desktop.
Save 3t14/d2375634fdbe03001b69374e5a595320 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using Plugin.Geolocator;
using Xamarin.Forms;
namespace HelloGeoLocator
{
public class App : Application
{
Label locationLabel = new Label
{
Text = "緯度 = 0, 軽度 = 0",
HorizontalTextAlignment = TextAlignment.Center
};
public App()
{
// The root page of your application
var content = new ContentPage
{
Title = "HelloGeoLocator",
Content = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
Children = {
new Label {
HorizontalTextAlignment = TextAlignment.Center,
Text = "位置情報"
},
locationLabel // 緯度、軽度お表示
}
}
};
MainPage = new NavigationPage(content);
}
protected override void OnStart()
{
// Handle when your app starts
updatePosition();
// 位置情報取得時に変化が生じた場合に情報を更新する
var locator = CrossGeolocator.Current;
locator.PositionChanged += (sender, e) =>
{
updatePosition();
};
}
public async void updatePosition()
{
try
{
var locator = CrossGeolocator.Current;
var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);
locationLabel.Text = string.Format(
"緯度 = {0},\n 軽度 = {1} ",
position.Latitude, position.Longitude);
}
catch (Exception ex)
{
locationLabel.Text = "取得できませんでした。";
Debug.WriteLine(ex.Message);
}
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment