Real time GPS UWP

0

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

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)

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;
        }
    }

}

}

background
geolocation
win-universal-app
asked on Stack Overflow Mar 24, 2016 by Setnipat Kriangsakdachai • edited Mar 24, 2016 by Setnipat Kriangsakdachai

1 Answer

0

Your Solution : Make 3 projects in your solution.

1> Background Task "references App_Code"

2> App_Code "contains calculations,mostly Backend 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

Scenario 1> App Open,User Requests Update

Trigger Your background Task from code behind.

Scenario 2> App Closed,Not Being Used

Run your background task!

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.

For real-time you could look at SignalR ( can't help any further here)

answered on Stack Overflow Mar 25, 2016 by Rahul Jha

User contributions licensed under CC BY-SA 3.0