Advertisement
Niyo

wp8LocationServices

Jan 19th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1.  
  2.         /// <summary>
  3.         /// Get actual Position from GPS/Wifi/gsm
  4.         /// </summary>
  5.         /// <returns></returns>
  6.         private async Task<Geoposition> getLocation(int maxAgeInMinutes = 5, int timeOutinSeconds = 10)
  7.         {
  8.             Geolocator geolocator = new Geolocator();
  9.  
  10.             Geoposition geoposition = null;
  11.  
  12.             if (geolocator.LocationStatus != PositionStatus.Disabled) // Check if we can access the LocationService
  13.             {
  14.                 geoposition = await geolocator.GetGeopositionAsync(
  15.                 maximumAge: TimeSpan.FromMinutes(maxAgeInMinutes),
  16.                 timeout: TimeSpan.FromSeconds(timeOutinSeconds)
  17.                 );
  18.             }
  19.  
  20.             return geoposition;
  21.         }
  22.  
  23.         /// <summary>
  24.         /// Get Coordinates from adress
  25.         /// </summary>
  26.         /// <param name="searchTerm"></param>
  27.         private async void getLocationCoordsfromAdress(string searchTerm)
  28.         {
  29.             GeocodeQuery a = new GeocodeQuery();
  30.  
  31.             Geoposition geoposition = await getLocation();
  32.  
  33.             if (geoposition != null)
  34.                 a.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
  35.             else
  36.                 a.GeoCoordinate = new GeoCoordinate();
  37.             a.SearchTerm = searchTerm; // mock search, replace with search value
  38.  
  39.             a.QueryCompleted += (s, e) =>
  40.             {
  41.                 if (e.Error == null && e.Result.Count > 0)
  42.                 {
  43.                     // DO SOMETHING SERIOUS
  44.                 }
  45.             };
  46.  
  47.             a.QueryAsync();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Get Location name by lat and long
  52.         /// </summary>
  53.         private async void getLocationName()
  54.         {
  55.             Geoposition geoposition = await getLocation(1);
  56.  
  57.             List<MapLocation> locations;
  58.             ReverseGeocodeQuery query = new ReverseGeocodeQuery();
  59.             query.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
  60.             query.QueryCompleted += (s, e) =>
  61.             {
  62.                 if (e.Error == null && e.Result.Count > 0)
  63.                 {
  64.                     locations = e.Result as List<MapLocation>;
  65.  
  66.                     Model.LocationName = locations[0].Information.Address.Street;
  67.                     // Do whatever you want with returned locations.
  68.                     // e.g. MapAddress address = locations[0].Information.Address;
  69.                 }
  70.             };
  71.             query.QueryAsync();
  72.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement