Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Get actual Position from GPS/Wifi/gsm
- /// </summary>
- /// <returns></returns>
- private async Task<Geoposition> getLocation(int maxAgeInMinutes = 5, int timeOutinSeconds = 10)
- {
- Geolocator geolocator = new Geolocator();
- Geoposition geoposition = null;
- if (geolocator.LocationStatus != PositionStatus.Disabled) // Check if we can access the LocationService
- {
- geoposition = await geolocator.GetGeopositionAsync(
- maximumAge: TimeSpan.FromMinutes(maxAgeInMinutes),
- timeout: TimeSpan.FromSeconds(timeOutinSeconds)
- );
- }
- return geoposition;
- }
- /// <summary>
- /// Get Coordinates from adress
- /// </summary>
- /// <param name="searchTerm"></param>
- private async void getLocationCoordsfromAdress(string searchTerm)
- {
- GeocodeQuery a = new GeocodeQuery();
- Geoposition geoposition = await getLocation();
- if (geoposition != null)
- a.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
- else
- a.GeoCoordinate = new GeoCoordinate();
- a.SearchTerm = searchTerm; // mock search, replace with search value
- a.QueryCompleted += (s, e) =>
- {
- if (e.Error == null && e.Result.Count > 0)
- {
- // DO SOMETHING SERIOUS
- }
- };
- a.QueryAsync();
- }
- /// <summary>
- /// Get Location name by lat and long
- /// </summary>
- private async void getLocationName()
- {
- Geoposition geoposition = await getLocation(1);
- List<MapLocation> locations;
- ReverseGeocodeQuery query = new ReverseGeocodeQuery();
- query.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
- query.QueryCompleted += (s, e) =>
- {
- if (e.Error == null && e.Result.Count > 0)
- {
- locations = e.Result as List<MapLocation>;
- Model.LocationName = locations[0].Information.Address.Street;
- // Do whatever you want with returned locations.
- // e.g. MapAddress address = locations[0].Information.Address;
- }
- };
- query.QueryAsync();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement