Send device's current location to Azure service in Windows 8.1

时光怂恿深爱的人放手 提交于 2019-12-12 04:18:43

问题


I am developing a location tracking windows mobile app and I want to send my current location to azure service as a background task. Getting current location is done. Currently I show the current location in the app tile. Likewise showing location in the tile I want to send location to the azure service. But how can I send my current location to azure service continuously so rest of the devices can see the device's location.

So far my code

public sealed class BackgroundGeofenceTask : IBackgroundTask
{

    //Azure Service
    //private MobileServiceCollection<Location, Location> items;
    //private IMobileServiceTable<Location> LocationTable = App.WayToSchool5Client.GetTable<Location>();


    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();

                    XmlDocument xml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text03);
                    var tileElements = xml.GetElementsByTagName("text");
                    tileElements[0].AppendChild(xml.CreateTextNode(pos.Coordinate.Point.Position.Latitude.ToString("f5")));
                    tileElements[1].AppendChild(xml.CreateTextNode(pos.Coordinate.Point.Position.Longitude.ToString("f5")));
                    tileElements[2].AppendChild(xml.CreateTextNode(pos.Coordinate.Point.Position.Altitude.ToString("f0")));
                    TileNotification tile = new TileNotification(xml);
                    TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();
                    updater.Update(tile);

                    //Send locationto azure
                    //var db = new Location { ServiceId = "sID", CurrentLatitude = pos.Coordinate.Point.Position.Longitude.ToString(), CurrentLongitude = pos.Coordinate.Point.Position.Longitude.ToString() };
                    //await LocationTable.InsertAsync(db);



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

Location.cs

public class Location
{
    public string ServiceId { get; set; }
    public string CurrentLatitude { get; set; }
    public string CurrentLongitude { get; set; }     
}

来源:https://stackoverflow.com/questions/34831107/send-devices-current-location-to-azure-service-in-windows-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!