实时GPS UWP [英] Real time GPS UWP
问题描述
我真的想知道我怎么可以更新地图的用户的位置,而UWP应用程序在运行bakground
I really want to know how do I can update the position of the user in the map while the UWP app was running in bakground
下面是我的code现在
Here is my code right now
private async void PinPoints()
{
//Pin point to the map
Windows.Devices.Geolocation.Geopoint position = await Library.Position();
double lat = position.Position.Latitude;
double lon = position.Position.Longitude;
//Geoposition alttest = await Library.Temp();
//alt = alttest.Coordinate.Altitude;
DependencyObject marker = Library.Marker(""
//+ Environment.NewLine + "Altitude " + alt
);
Display.Children.Add(marker);
Windows.UI.Xaml.Controls.Maps.MapControl.SetLocation(marker, position);
Windows.UI.Xaml.Controls.Maps.MapControl.SetNormalizedAnchorPoint(marker, new Point(0.5, 0.5));
Display.LandmarksVisible = true;
Display.ZoomLevel = 16;
Display.Center = position;
}
此功能将精确定位当前位置对我来说,但是当用户打开这个页面,由于我已经把它在公共地图(它只会做){}
This function will pinpoint the current location for me but it will do only when user open this page due to I've put it in the public Map() {}
当前:获取位置时打开地图页面,当我走在地图仍然是同一个地方
我要什么:位置保持,而我继续前进,也在后台运行(如果应用还改变了接近的位置数据)更改
Current : Get the location when open map page and when I walk the map still be the same place What I want : The position keep changing while I move on and also run on background (If application is close location data still changed)
有没有code。如果我要补充code我应该在哪里解决,并解决这个选址问题我该怎么办?
Is there any code to solve this location problem if I have to add code where should I fix and what should I do?
其他我现在执行的背景(不知道是它的工作或没有)用这样的类创建窗口运行时组件(通用)
Additional now I perform the background (Not sure is it work or not) by create the Window Runtime Component (Universal) with class like this
*我已经把这个项目作为主要的一个参考。
*I already put this project as the reference of the main one
namespace BackgroundRunning
{
public sealed class TaskBG : IBackgroundTask
{
BackgroundTaskDeferral _deferral = null;
Accelerometer _accelerometer = null;
Geolocator _locator = new Geolocator();
public void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
try
{
// force gps quality readings
_locator.DesiredAccuracy = PositionAccuracy.High;
taskInstance.Canceled += taskInstance_Canceled;
_accelerometer = Windows.Devices.Sensors.Accelerometer.GetDefault();
_accelerometer.ReportInterval = _accelerometer.MinimumReportInterval > 5000 ? _accelerometer.MinimumReportInterval : 5000;
_accelerometer.ReadingChanged += accelerometer_ReadingChanged;
}
catch (Exception ex)
{
// Add your chosen analytics here
System.Diagnostics.Debug.WriteLine(ex);
}
}
void taskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
_deferral.Complete();
}
async void accelerometer_ReadingChanged(Windows.Devices.Sensors.Accelerometer sender, Windows.Devices.Sensors.AccelerometerReadingChangedEventArgs args)
{
try
{
if (_locator.LocationStatus != PositionStatus.Disabled)
{
try
{
Geoposition pos = await _locator.GetGeopositionAsync();
}
catch (Exception ex)
{
if (ex.HResult != unchecked((int)0x800705b4))
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
public void Dispose()
{
if (_accelerometer != null)
{
_accelerometer.ReadingChanged -= accelerometer_ReadingChanged;
_accelerometer.ReportInterval = 0;
}
}
}
}
推荐答案
您的解决方案:
使您的解决方案3的项目。
Your Solution : Make 3 projects in your solution.
1>后台任务引用APP_ code
1> Background Task "references App_Code"
2> APP_ code中包含的计算,主要是后端code
2> App_Code "contains calculations,mostly Backend Code"
3>地图(主项目)引用APP_ code
3> Map(Main Project) "references App_Code"
注册一个后台任务到项目,并指定在这之后应该再次运行的时间间隔
Register a background Task to your project and specify the time interval after which it should run again
方案1> 应用打开,用户请求更新
从code背后触发后台任务。
Trigger Your background Task from code behind.
方案2> 应用闭,不使用
运行你的后台任务!
所以基本上保持你的backgroundTask简单(使它在一个类的run方法,你只需要调用合适的APP_ code类方法),并要在后台发生的所有计算,让他们在APP_ code 。另外,如果我没有搞错这之间的后台任务是由自身触发的最小时间间隔不能设置低于15分钟。
So basically keep your backgroundTask simple(make it a class in whose run method you just call the proper App_Code Classes Method) and all calculations that you want to happen in the background keep them in App_Code. Also, if I am no wrong the minimum interval between which a background Task is triggered by itself cannot be set below 15 minutes.
有关实时你可以看看SignalR(不能帮助任何进一步这里)
For real-time you could look at SignalR ( can't help any further here)
这篇关于实时GPS UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!