return;
}
- this.GetLocation();
+ if (!StoredLocation.IsThereCurrentLocation)
+ {
+ this.GetLocation();
+ }
+ else
+ {
+ GeoCoordinate geoCoordinate = CoordinateHelper.GetStoredGeoCoordinate();
+
+ this.UpdateMap(geoCoordinate, StoredLocation.City, StoredLocation.Country);
+ }
}
private async void GetLocation()
maximumAge: TimeSpan.FromMinutes(5),
timeout: TimeSpan.FromSeconds(10)
);
- GeoCoordinate currentGeoCoordinate = CoordinateConverter.ConvertGeocoordinate(geoposition.Coordinate);
+ GeoCoordinate currentGeoCoordinate = CoordinateHelper.ConvertGeocoordinate(geoposition.Coordinate);
- ReverseGeocodeQuery currentReverseGeocodeQuery = new ReverseGeocodeQuery();
- currentReverseGeocodeQuery.GeoCoordinate = currentGeoCoordinate;
- currentReverseGeocodeQuery.QueryCompleted += QueryCompletedCallback;
- currentReverseGeocodeQuery.QueryAsync();
+ ReverseGeocodeAndUpdateMap(currentGeoCoordinate);
}
catch (Exception ex)
{
}
}
+ // TODO: problems updating Map because this method may be called when automatically retrieving
+ // the current user's location or when user taps on map tyring to choose by herself her location.
+ // There could be 2 threads updating Map at the same time. Solution: remove the feature
+ // of getting the current user's location (user must always choose her/his location instead of doing
+ // it automatically)
+ private void ReverseGeocodeAndUpdateMap(GeoCoordinate currentGeoCoordinate)
+ {
+ ReverseGeocodeQuery currentReverseGeocodeQuery = new ReverseGeocodeQuery();
+ currentReverseGeocodeQuery.GeoCoordinate = currentGeoCoordinate;
+ currentReverseGeocodeQuery.QueryCompleted += QueryCompletedCallback;
+ currentReverseGeocodeQuery.QueryAsync();
+ }
private void QueryCompletedCallback(object sender, QueryCompletedEventArgs<IList<MapLocation>> eventData)
{
{
if (eventData.Result.Count > 0)
{
+ // TODO: What if there is no city or country. Is there null value or empty string?
MapAddress address = eventData.Result[0].Information.Address;
GeoCoordinate currentGeoCoordinate = eventData.Result[0].GeoCoordinate;
- string cityCountry = String.Format(CultureInfo.InvariantCulture, "{0}, {1}", address.City, address.Country);
- this.LocationTextCityCountry.Text = cityCountry;
-
- // TODO: If I want to store more than one place I should use a database :(
- StoredLocation.CurrentLatitude = currentGeoCoordinate.Latitude;
- StoredLocation.CurrentLongitude = currentGeoCoordinate.Longitude;
- // TODO: If I want to store more thant one place I should use a database :(
- StoredLocation.City = address.City;
- StoredLocation.Country = address.Country;
- StoredLocation.IsNewLocation = true;
-
- // Create a small circle to mark the current location.
- Ellipse myCircle = new Ellipse();
- myCircle.Fill = new SolidColorBrush(Colors.Blue);
- myCircle.Height = 20;
- myCircle.Width = 20;
- myCircle.Opacity = 50;
-
- // Create a MapOverlay to contain the circle.
- MapOverlay myLocationOverlay = new MapOverlay();
- myLocationOverlay.Content = myCircle;
- myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
- myLocationOverlay.GeoCoordinate = currentGeoCoordinate;
-
- // Create a MapLayer to contain the MapOverlay.
- MapLayer myLocationLayer = new MapLayer();
- myLocationLayer.Add(myLocationOverlay);
-
- this.mapWeatherInformation.Center = currentGeoCoordinate;
- this.mapWeatherInformation.ZoomLevel = 13;
-
- // Add the MapLayer to the Map.
- this.mapWeatherInformation.Layers.Add(myLocationLayer);
+ UpdateMap(currentGeoCoordinate, address.City, address.Country);
}
}
}
+ private void UpdateMap(GeoCoordinate geoCoordinate, string city, string country)
+ {
+ // Create a small circle to mark the current location.
+ Ellipse myCircle = new Ellipse();
+ myCircle.Fill = new SolidColorBrush(Colors.Blue);
+ myCircle.Height = 20;
+ myCircle.Width = 20;
+ myCircle.Opacity = 50;
+
+ // Create a MapOverlay to contain the circle.
+ MapOverlay myLocationOverlay = new MapOverlay();
+ myLocationOverlay.Content = myCircle;
+ myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
+ myLocationOverlay.GeoCoordinate = geoCoordinate;
+
+ // Create a MapLayer to contain the MapOverlay.
+ MapLayer myLocationLayer = new MapLayer();
+ myLocationLayer.Add(myLocationOverlay);
+
+ this.mapWeatherInformation.Layers.Clear();
+
+ this.mapWeatherInformation.Center = geoCoordinate;
+ this.mapWeatherInformation.ZoomLevel = 13;
+
+ // TODO: What if there is no city or country. Is there null value or empty string?
+ string cityCountry = String.Format(CultureInfo.InvariantCulture, "{0}, {1}", city, country);
+ this.LocationTextCityCountry.Text = cityCountry;
+ // Add the MapLayer to the Map.
+ this.mapWeatherInformation.Layers.Add(myLocationLayer);
+ }
- public static class CoordinateConverter
+ private static class CoordinateHelper
{
public static GeoCoordinate ConvertGeocoordinate(Geocoordinate geocoordinate)
{
geocoordinate.Heading ?? Double.NaN
);
}
+
+ // TODO: database
+ // TODO: What if Double.NAN or null... Am I going to have some problem storing data in IsolatedStorageSettings?
+ public static void StoreGeoCoordinate(GeoCoordinate geocoordinate)
+ {
+ StoredLocation.CurrentLatitude = geocoordinate.Latitude;
+ StoredLocation.CurrentLongitude = geocoordinate.Longitude;
+ StoredLocation.IsNewLocation = true;
+ StoredLocation.CurrentAltitude = geocoordinate.Altitude;
+ StoredLocation.CurrentHorizontalAccuracy = geocoordinate.HorizontalAccuracy;
+ StoredLocation.CurrentVerticalAccuracy = geocoordinate.VerticalAccuracy;
+ StoredLocation.CurrentSpeed = geocoordinate.Speed;
+ StoredLocation.CurrentCourse = geocoordinate.Course;
+ }
+
+ public static GeoCoordinate GetStoredGeoCoordinate()
+ {
+ return new GeoCoordinate
+ (
+ StoredLocation.CurrentLatitude,
+ StoredLocation.CurrentLongitude,
+ StoredLocation.CurrentAltitude,
+ StoredLocation.CurrentHorizontalAccuracy,
+ StoredLocation.CurrentVerticalAccuracy,
+ StoredLocation.CurrentSpeed,
+ StoredLocation.CurrentCourse
+ );
+ }
+ }
+
+ private void mapWeatherInformation_Tap(object sender, System.Windows.Input.GestureEventArgs e)
+ {
+ var point = e.GetPosition(this.mapWeatherInformation);
+ GeoCoordinate geocoordinate = this.mapWeatherInformation.ConvertViewportPointToGeoCoordinate(point);
+ ReverseGeocodeAndUpdateMap(geocoordinate);
}
+
+ private void SaveLocationButton_Click(object sender, RoutedEventArgs e)
+ {
+ // TODO: Could there some problem if user clicks button and thread is in this very moment updating map?
+ var geoCoordinate = this.mapWeatherInformation.Center;
+ // TODO: What if there is no city or country. Is there null value or empty string?
+ //StoredLocation.City = address.City;
+ //StoredLocation.Country = address.Country;
+ CoordinateHelper.StoreGeoCoordinate(geoCoordinate);
+ }
}
}
\ No newline at end of file
namespace WeatherInformation.Model.Services
{
// TODO: If I want to store more than one place I should use a database :(
+ // TODO: What if Double.NAN or null... Am I going to have some problem storing data in IsolatedStorageSettings?
class StoredLocation
{
public static double CurrentLatitude
}
}
+ public static double CurrentAltitude
+ {
+ get
+ {
+ if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentAltitude"))
+ {
+ return (double)IsolatedStorageSettings.ApplicationSettings["CurrentAltitude"];
+ }
+ // TODO: what if settings does not contain this value? :/
+ return 0;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["CurrentAltitude"] = value;
+ }
+ }
+
+ public static double CurrentHorizontalAccuracy
+ {
+ get
+ {
+ if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentHorizontalAccuracy"))
+ {
+ return (double)IsolatedStorageSettings.ApplicationSettings["CurrentHorizontalAccuracy"];
+ }
+ // TODO: what if settings does not contain this value? :/
+ return 0;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["CurrentHorizontalAccuracy"] = value;
+ }
+ }
+
+ public static double CurrentVerticalAccuracy
+ {
+ get
+ {
+ if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentVerticalAccuracy"))
+ {
+ return (double)IsolatedStorageSettings.ApplicationSettings["CurrentVerticalAccuracy"];
+ }
+ // TODO: what if settings does not contain this value? :/
+ return 0;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["CurrentVerticalAccuracy"] = value;
+ }
+ }
+
+ public static double CurrentSpeed
+ {
+ get
+ {
+ if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentSpeed"))
+ {
+ return (double)IsolatedStorageSettings.ApplicationSettings["CurrentSpeed"];
+ }
+ // TODO: what if settings does not contain this value? :/
+ return 0;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["CurrentSpeed"] = value;
+ }
+ }
+
+ public static double CurrentCourse
+ {
+ get
+ {
+ if (IsolatedStorageSettings.ApplicationSettings.Contains("CurrentCourse"))
+ {
+ return (double)IsolatedStorageSettings.ApplicationSettings["CurrentCourse"];
+ }
+ // TODO: what if settings does not contain this value? :/
+ return 0;
+ }
+ set
+ {
+ IsolatedStorageSettings.ApplicationSettings["CurrentCourse"] = value;
+ }
+ }
+
public static string City
{
get