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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
´╗┐using SmartCharging.Converter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Services.Maps;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Maps;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SmartCharging
{
    public sealed partial class MapAddressSelectorControl : UserControl
    {   

       private DispatcherTimer townTextBoxTimer = new DispatcherTimer();
       private DispatcherTimer addressTextBoxTimer = new DispatcherTimer();
       public MapLocation siteLocation { get; private set; }
       public string addressString
       {
           get { return this.AddressTextbox.Text; }
           private set { var s = ""; }
       }

       public event PropertyChangedEventHandler PropertyChanged;
       public MapLocation townLocation {get; private set;}
       

      

        public MapAddressSelectorControl()
        {
            this.InitializeComponent();
            townTextBoxTimer.Tick += TownTextbox_TimerEventHandler;
            townTextBoxTimer.Interval = new TimeSpan(0, 0, 0, 2);

            addressTextBoxTimer.Tick += AddressTextbox_TimerEventHandler;
            addressTextBoxTimer.Interval = new TimeSpan(0, 0, 0, 2);
            this.GridContainer.DataContext = this;
            this.AddressTextbox.DataContext = this;
           
        }



        private void CityListItem_Tapped(object sender, TappedRoutedEventArgs e)
        {
            CitySelectionFlyout.Hide();
            this.MapControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            this.TownTextBox.Text = (sender as ListBoxItem).Content as string;
            MapLocation mapLocation = ((sender as Control).DataContext as MapLocation);
            this.townLocation = mapLocation;
            MapControl.Center = mapLocation.Point;
            MapControl.ZoomLevel = 5;
            this.AddressTextbox.IsEnabled = true;
            

        }

        private void AddressListItem_Tapped(object sender, TappedRoutedEventArgs e)
        {
            this.AddressSelectionFlyout.Hide();
            this.MapControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            Regex rgx = new Regex("\r|\n");
            this.AddressTextbox.Text = rgx.Replace(((sender as ListBoxItem).Content as string), "");
            MapLocation mapLocation = ((sender as Control).DataContext as MapLocation);
            this.siteLocation = mapLocation;

            this.addMapIcon(mapLocation.Point);
            MapControl.Center = mapLocation.Point;
            MapControl.ZoomLevel = 15;
        }

        private void  addMapIcon(Geopoint point){
            MapIcon MapIcon1 = new MapIcon();
            MapIcon1.Location = point;
            MapIcon1.NormalizedAnchorPoint = new Point(1.0, 0.5);
            MapIcon1.Visible = true;
            MapIcon1.ZIndex = int.MaxValue;
            MapIcon1.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/position.png"));
            MapControl.MapElements.Clear();
            MapControl.MapElements.Add(MapIcon1);
        }



        private void TownTextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.TownTextBox.FocusState == Windows.UI.Xaml.FocusState.Pointer)
            {
                townTextBoxTimer.Stop();
                townTextBoxTimer.Start();
            }
           
            
        }

        private void AddressTextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.AddressTextbox.FocusState == Windows.UI.Xaml.FocusState.Pointer)
            {
                addressTextBoxTimer.Stop();
                addressTextBoxTimer.Start();
            }
            
        }

        private async void TownTextbox_TimerEventHandler(object sender, object e)
        {
            string searchString = this.TownTextBox.Text;

            if (searchString.Length >= 3) {
                this.showLoading();
                townTextBoxTimer.Stop();
                MapLocationFinderResult result = await this.GetLocationByString(searchString, null);
                if (result.Status == MapLocationFinderStatus.Success)
                {
                    List<MapLocation> locations = new List<MapLocation>();
                    foreach (MapLocation mapLocation in result.Locations)
                    {
                        if (mapLocation.Address.Town.Contains(searchString))
                            locations.Add(mapLocation);
                    }
                    // Bind the location list to the ListView control.

                    CityList.ItemsSource = locations;
                    FlyoutBase.ShowAttachedFlyout((FrameworkElement)this.TownTextBox);
                }
                else
                {
                    // Tell the user to try again.
                }

                this.hideLoading();
            }
            

        }


        private async void AddressTextbox_TimerEventHandler(object sender, object e)
        {
            string searchString = this.AddressTextbox.Text;

            if (searchString.Length >= 3)
            {
                this.showLoading();
                addressTextBoxTimer.Stop();
                MapLocationFinderResult result = await this.GetLocationByString(searchString, this.townLocation.Point);
                if (result.Status == MapLocationFinderStatus.Success)
                {
                    List<MapLocation> locations = new List<MapLocation>();
                    foreach (MapLocation mapLocation in result.Locations)
                    {
                     
                        locations.Add(mapLocation);
                    }
                    // Bind the location list to the ListView control.

                    AddressList.ItemsSource = locations;
                    FlyoutBase.ShowAttachedFlyout((FrameworkElement)this.AddressTextbox);
                }
                else
                {
                    // Tell the user to try again.
                }

                this.hideLoading();
            }


        }

        private async Task<MapLocationFinderResult> GetLocationByString(string location, Geopoint startingPosition){
           

            MapLocationFinderResult result = await MapLocationFinder.FindLocationsAsync(location, startingPosition, 50);

            return result;
        }

        public async Task SetInitialPosition(Geopoint position)
        {
            MapLocationFinderResult result = await MapLocationFinder.FindLocationsAtAsync(position);

            // If the query returns results, display the name of the town
            // contained in the address of the first result.
            if (result.Status == MapLocationFinderStatus.Success)
            {
                AddressToStringConverter aTsC = new AddressToStringConverter();
                MapLocation ml = result.Locations[0];
                this.townLocation = ml;
                this.siteLocation = ml;

                this.TownTextBox.Text =ml.Address.Town + ", " + ml.Address.Country;
                this.AddressTextbox.Text = (string)aTsC.Convert(ml.Address, null, null, null);
                this.AddressTextbox.IsEnabled = true;
                this.addMapIcon(ml.Point);
                MapControl.Center = ml.Point;
                MapControl.ZoomLevel = 15;
            }
        }

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }

        private void showLoading()
        {
            this.Loader.Visibility = Visibility.Visible;
        }

        private void hideLoading()
        {
            this.Loader.Visibility = Visibility.Collapsed;
        }



 

       

        
    }
}

Commits for Nextrek/Android/SmartCharging/SmartCharging_WP/SmartCharging/MapAddressSelectorControl.xaml.cs

Diff revisions: vs.
Revision Author Commited Message
752 Diff Diff JMBauan picture JMBauan Wed 16 Sep, 2015 20:06:56 +0000

upload Avatar
Edit site

731 Diff Diff JMBauan picture JMBauan Tue 15 Sep, 2015 10:43:57 +0000
692 Diff Diff JMBauan picture JMBauan Sun 06 Sep, 2015 22:20:31 +0000
691 JMBauan picture JMBauan Sat 05 Sep, 2015 18:08:58 +0000

location selector