Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources;
using Windows.Devices.Geolocation;
using Windows.System;
using Windows.UI.Popups;

namespace SmartCharging.Common
{
    class UserLocationHelper
    {
        private ResourceLoader resourceLoader;
        private Config config;
        private ErrorHandler errorHandler;

       public  UserLocationHelper()
        {
            resourceLoader = ResourceLoader.GetForCurrentView("Resources");
            config = Config.Instance;
            errorHandler = new ErrorHandler();
        }

        public async Task<bool> AskForGps()
        {

            MessageDialog dialogbox = new MessageDialog(resourceLoader.GetString("GPSRequestMessage"));


            //OK Button
            UICommand okBtn = new UICommand(resourceLoader.GetString("OK"));
            okBtn.Invoked = (s) =>
            {
                Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
            };
            dialogbox.Commands.Add(okBtn);


            //Show message
            await dialogbox.ShowAsync();
            return true;

        }

        public async Task<Geopoint> GetUserLocation()
        {
            /*get user's position; 
             * if this operation succeeds
             *  save the position and continue
             * else
             *  retrieve the last save position
             *  if no last saved position
             *   show error message
             *  else
             *   continue
             */
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            Geolocator geolocator = new Geolocator();
            Geopoint ret;
            geolocator.DesiredAccuracyInMeters = 50;
            try
            {
                Geoposition geoposition = await geolocator.GetGeopositionAsync(
                    maximumAge: TimeSpan.FromMinutes(5),
                    timeout: TimeSpan.FromSeconds(6)
                    );

                ret = new Geopoint(new BasicGeoposition()
                {
                    Latitude = geoposition.Coordinate.Point.Position.Latitude,
                    Longitude = geoposition.Coordinate.Point.Position.Longitude
                });
                this.SavePosition(ret);

            }
            catch
            {
                ret = this.GetLastSavedPosition();
            }


            return ret;
        }

        private Geopoint GetLastSavedPosition()
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            string toBeParsed = (string)localSettings.Values["LastSavedPostition"];
            Geopoint ret = null;
            if (toBeParsed != null && !toBeParsed.Equals(""))
            {
                ret = new Geopoint(new BasicGeoposition()
                {
                    Latitude = double.Parse(toBeParsed.Split('|')[0]),
                    Longitude = double.Parse(toBeParsed.Split('|')[1])
                });
            }
            return ret;
        }

        private void SavePosition(Geopoint position)
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

            localSettings.Values["LastSavedPostition"] = position.Position.Latitude + "|" + position.Position.Longitude;



        }
    }
}

Commits for Nextrek/Android/SmartCharging/SmartCharging_WP/SmartCharging/Common/UserLocation.cs

Diff revisions: vs.
Revision Author Commited Message
691 JMBauan picture JMBauan Sat 05 Sep, 2015 18:08:58 +0000

location selector