async void SetUserTrackCurrentLocation(double latitude, double longitude) { try { CloseMessage(); // trvMap.Children.Clear(); // Get the cancellation token. _cts = new CancellationTokenSource(); token = _cts.Token; // Get the location. Location location = new Location(latitude, longitude); //// Now set the zoom level of the map based on the accuracy of our location data. //// Default to IP level accuracy. We only show the region at this level - No icon is displayed. //double zoomLevel = 13.0f; //// Add the 10m icon and zoom closer. //if (trvMap.Children.Count == 0) { // trvMap.Children.Add(_locationIcon10m); //} //MapLayer.SetPosition(_locationIcon10m, location); //zoomLevel = 15.0f; //// Set the map to the given location and zoom level. //trvMap.SetView(location, zoomLevel); // Now set the zoom level of the map based on the accuracy of our location data. // Default to IP level accuracy. We only show the region at this level - No icon is displayed. double zoomLevel = 17.0f; LocationIcon10m _locationUserIcon10m = new LocationIcon10m(); Callout callout = new Callout(); callout.Text = "Tracker's Location"; callout.Lon = "Lon (λ): " + location.Longitude.ToString().Substring(0, 7); callout.Lat = "Lat (φ): " + location.Latitude.ToString().Substring(0, 7); _locationUserIcon10m.DataContext = callout; foreach (var children in trvMap.Children) { if (children.GetType().Name == "LocationIcon10m") { trvMap.Children.Remove(children); break; } } // Add the 10m icon and zoom closer. trvMap.Children.Add(_locationUserIcon10m); MapLayer.SetPosition(_locationUserIcon10m, location); // Set the map to the given location and zoom level. trvMap.SetView(location, zoomLevel); } catch { } }
async Task SetCurrentLocation() { try { // Get the cancellation token. _cts = new CancellationTokenSource(); token = _cts.Token; // Get the location. var asyncResult = _geolocator.GetGeopositionAsync(); var task = asyncResult.AsTask(); var readyTask = await Task.WhenAny(task, Task.Delay(10000)); if (readyTask != task) { SetMessage(MessageType.Error, "❎ Unable to find your location, trying again..."); SetCurrentLocation(); } Geoposition pos = await task; // MessageTextbox.Text = ""; Location location = new Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude); ProxyTracker.GetInstance().MyTrackLocation = new Services.TrackService.TrackLocation { Latitude = pos.Coordinate.Latitude, Longitude = pos.Coordinate.Longitude }; // Now set the zoom level of the map based on the accuracy of our location data. // Default to IP level accuracy. We only show the region at this level - No icon is displayed. double zoomLevel = 13.0f; Callout callout = new Callout(); callout.Text = "My Location"; callout.Lon = "Lon (λ): " + location.Longitude.ToString().Substring(0, 7); callout.Lat = "Lat (φ): " + location.Latitude.ToString().Substring(0, 7); _locationIcon100m.DataContext = callout; foreach (var children in trvMap.Children) { if (children.GetType().Name == "LocationIcon100m") { trvMap.Children.Remove(children); break; } } // Add the 100m icon and zoom a little closer. trvMap.Children.Add(_locationIcon100m); MapLayer.SetPosition(_locationIcon100m, location); zoomLevel = 17.0f; // Set the map to the given location and zoom level. trvMap.SetView(location, zoomLevel); // txtMessage.Text = ""; } catch (Exception ex) { SetMessage(MessageType.Error, "❎ Location services is disabled on this computer"); } }