private void TrackLocation_Click_1(object sender, EventArgs e)
{
if (!tracking)
{
App.Geolocator = new Geolocator();
App.Geolocator.DesiredAccuracy = PositionAccuracy.High;
App.Geolocator.MovementThreshold = 2; // The units are meters.
void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
string status = "";
switch (args.Status)
{
case PositionStatus.Disabled:
// the application does not have the right capability or the location master switch is off
status = "location is disabled in phone settings";
break;
case PositionStatus.Initializing:
// the geolocator started the tracking operation
status = "initializing";
break;
case PositionStatus.NoData:
// the location service was not able to acquire the location
status = "no data";
break;
case PositionStatus.Ready:
// the location service is generating geopositions as specified by the tracking parameters
status = "ready";
break;
case PositionStatus.NotAvailable:
status = "not available";
// not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
break;
case PositionStatus.NotInitialized:
// the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
break;
}
Dispatcher.BeginInvoke(() =>
{
StatusTextBlock.Text = status;
});
}
void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
string status = "";
switch (args.Status)
{
case PositionStatus.Disabled:
// the application does not have the right capability or the location master switch is off
status = "location is disabled in phone settings";
break;
case PositionStatus.Initializing:
// the geolocator started the tracking operation
status = "initializing";
break;
case PositionStatus.NoData:
// the location service was not able to acquire the location
status = "no data";
break;
case PositionStatus.Ready:
// the location service is generating geopositions as specified by the tracking parameters
status = "ready";
break;
case PositionStatus.NotAvailable:
status = "not available";
// not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
break;
case PositionStatus.NotInitialized:
// the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
break;
}
Dispatcher.BeginInvoke(() =>
{
StatusTextBlock.Text = status;
});
if (App.RunningInBackground)
{
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Title = "Location: ";
toast.Content = args.Status.ToString();
toast.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative);
toast.Show();
}
}
和 PositionChanged
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
if (App.RunningInBackground)
{
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
toast.Title = "Location is ";
toast.Content = "Latitude: " + args.Position.Coordinate.Latitude.ToString("0.00") + " Longitude: " + args.Position.Coordinate.Longitude.ToString("0.00");
toast.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative);
toast.Show();
}
Dispatcher.BeginInvoke(() =>
{
if (!MyMap.MapElements.Contains(MPL))
MyMap.MapElements.Add(MPL);
CurrenLocation = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
MPL.Path.Add(CurrenLocation);
MyMap.SetView(CurrenLocation, 15, MapAnimationKind.Parabolic);
MyMap.Layers.Clear();
MapOverlay MyOverlay = new MapOverlay();
MyOverlay.Content = GetGrid();
MyOverlay.GeoCoordinate = new GeoCoordinate(CurrenLocation.Latitude, CurrenLocation.Longitude);
MyOverlay.PositionOrigin = new Point(0, 0.5);
MapLayer MyLayer = new MapLayer();
MyLayer.Add(MyOverlay);
MyMap.Layers.Add(MyLayer);
});
}
之后我们启动应用程序
打开模拟器的 Additional tools中的location来模拟地理位置的变化,当然实现要把我们的程序切如后台。
运行效果如下:
好了相信大家看过之后在 windows phone 8 中实现一个基于定位的后台应用已经有了一个了解,欢迎大家在这里和我沟通交流或者在新浪微博上 @王博_Nick