示例#1
0
 private async Task <Geoposition> GetGeoposition()
 {
     if (geolocator == null)
     {
         return(null);
     }
     return(await geolocator?.GetGeopositionAsync());
 }
示例#2
0
        public static async Task <Geoposition> GetLocation()
        {
            var accessStatus = await Geolocator.RequestAccessAsync();

            if (accessStatus == GeolocationAccessStatus.Allowed)
            {
                var geolocator = new Geolocator {
                    DesiredAccuracyInMeters = 1000
                };
                var pos = await geolocator.GetGeopositionAsync();

                return(pos);
            }
            throw new Exception("Locating Failed");
        }
        public async Task <Geoposition> GetUserLocation(PositionAccuracy accuracy)
        {
            // Get cancellation token
            _cts = new CancellationTokenSource();
            CancellationToken token = _cts.Token;

            // Carry out the operation
            // geolocator and location permissions are initialized and checked on page creation.
            var geolocator = new Geolocator();

            // Request a high accuracy position for better accuracy locating the geofence
            geolocator.DesiredAccuracy = accuracy;

            return(await geolocator.GetGeopositionAsync().AsTask(token));
        }
示例#4
0
文件: App.xaml.cs 项目: User51342/Dev
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
            this.Resuming   += OnResuming;
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <Signal, SignalService.SignalDto>().ConvertUsing(new SignalConvert());
                cfg.CreateMap <WiFiSignal, SignalService.WifiSignalDto>().ConvertUsing(new WifiSignalsConvert());
            });
            Mapper.AssertConfigurationIsValid();
            Geolocator geolocator = new Geolocator();

            geolocator.GetGeopositionAsync();
        }
示例#5
0
        async Task <Geoposition> GetUsersPositionAsync()
        {
            Geoposition position = null;

            var status = await Geolocator.RequestAccessAsync();

            if (status == GeolocationAccessStatus.Allowed)
            {
                Geolocator locator = new Geolocator();

                // Where's the user?
                position = await locator.GetGeopositionAsync();
            }
            return(position);
        }
        private async Task <Geoposition> GetInnerGeoPositionAsync()
        {
            if (!HasUserOptedIn() || _geolocator.LocationStatus == PositionStatus.Disabled)
            {
                return(null);
            }


            _geolocator.DesiredAccuracyInMeters = 10;

            Geoposition geo = await _geolocator.GetGeopositionAsync();


            return(geo);
        }
示例#7
0
        //TODO: add insert data method

        #endregion



        #region Positioning
        private async void getCurrentPosition()
        {
            if (currentPosition == null)
            {
                currentPosition = new Position();
            }
            Geolocator  geo    = new Geolocator();
            Position    result = new Position();
            Geoposition pos    = await geo.GetGeopositionAsync();

            currentPosition.Longitude = pos.Coordinate.Point.Position.Longitude;
            currentPosition.Latitude  = pos.Coordinate.Point.Position.Latitude;
            LatitudeTextBlock.Text    = currentPosition.Latitude.ToString();
            LongitudeTextBlock.Text   = currentPosition.Longitude.ToString();
        }
示例#8
0
        async private void FindCurrentLocation()
        {
            var currentPosition = await _geolocator.GetGeopositionAsync();



            var locationFound = new Location
            {
                Latitude  = currentPosition.Coordinate.Latitude,
                Longitude = currentPosition.Coordinate.Longitude
            };

            MapLayer.SetPosition(pin, locationFound);
            map.SetView(locationFound, 15.0f);
        }
示例#9
0
        public IObservable <IGpsReading> GetLastReading() => Observable.FromAsync(async ct =>
        {
            var geolocator = new Geolocator();
            geolocator.AllowFallbackToConsentlessPositions();
            var location = await geolocator
                           .GetGeopositionAsync()
                           .AsTask(ct);

            if (location?.Coordinate == null)
            {
                return(null);
            }

            return(new GpsReading(location.Coordinate));
        });
示例#10
0
        public async static Task <Geoposition> GetPosition()
        {
            var accessStatus = await Geolocator.RequestAccessAsync();

            if (accessStatus != GeolocationAccessStatus.Allowed)
            {
                throw new Exception();
            }
            var geolocator = new Geolocator {
                DesiredAccuracyInMeters = 0
            };
            var position = await geolocator.GetGeopositionAsync();

            return(position);
        }
示例#11
0
        //private async  void AddNote_Loaded(object setter, NavigationEventArgs e)
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            //map service token from bing maps
            Map.MapServiceToken = "TOfdeDRJwGImuiaL7W88~SlMn6bqFU6GCmOupV2lB-g~AsoKvGZZisSkTudn4cs2nwQzmGl7eS58HojnUjiskKBwBEYkH-7yUV7sRAXsADIr";
            //set map style for add note feature.
            Map.Style = MapStyle.AerialWithRoads;
            Geopoint pos;

            if (e.Parameter == null)
            {
                viewing = false;

                var location = new Geolocator();
                location.DesiredAccuracyInMeters = 50;
                var position = await location.GetGeopositionAsync();

                pos = position.Coordinate.Point;
            }
            else
            {
                viewing = true;

                myText            = (MyMapNote)e.Parameter;
                titleTextBox.Text = myText.Title;
                noteTextBox.Text  = myText.Note;
                //if viewing =true then we are in the delete mode
                // and add button content will change to delete
                // and the function will change to a delete function
                addBtn.Content = "Delete";

                var myPos = new Windows.Devices.Geolocation.BasicGeoposition();
                myPos.Latitude  = myText.Latitude;
                myPos.Longitude = myText.Longitude;

                //drop a ancor pin to mark center point
                pos = new Geopoint(myPos);
                MapIcon icon = new MapIcon();
                icon.Location = pos;
                icon.NormalizedAnchorPoint = new Point(0.5, 1.0);
                icon.Title  = "Note Location";
                icon.Image  = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Pin.png"));
                icon.ZIndex = 0;
                Map.MapElements.Add(icon);
                Map.Center = pos;
            }

            await Map.TrySetViewAsync(pos, 14D);
        }
示例#12
0
        private async Task AccesstoLocate()
        {
            try
            {
                var accessStatus = await Geolocator.RequestAccessAsync();

                if (accessStatus == GeolocationAccessStatus.Allowed)
                {
                    LocateAllowed.Begin();
                    if (Context.EnablePosition)
                    {
                        try
                        {
                            _geolocator = new Geolocator();

                            ShowRefreshing();
                            pos = await _geolocator.GetGeopositionAsync();

                            if ((_geolocator.LocationStatus != PositionStatus.NoData) && (_geolocator.LocationStatus != PositionStatus.NotAvailable) && (_geolocator.LocationStatus != PositionStatus.Disabled))
                            {
                                await UpdatePosition(pos);
                            }
                            else
                            {
                                DeniePos();
                            }
                            _geolocator.StatusChanged += OnStatusChanged;
                        }
                        catch (Exception)
                        {
                            DeniePos();
                        }
                    }
                    else
                    {
                        HidePos();
                    }
                }
                else
                {
                    DeniePos();
                }
            }
            catch (Exception)
            {
                DeniePos();
            }
        }
        /// <summary>
        /// Helper method to invoke Geolocator.GetGeopositionAsync.
        /// </summary>
        async private void GetGeopositionAsync()
        {
            rootPage.NotifyUser("Checking permissions...", NotifyType.StatusMessage);

            try
            {
                // Get cancellation token
                cts = new CancellationTokenSource();
                CancellationToken token = cts.Token;

                // Carry out the operation
                Geoposition pos = await geolocator.GetGeopositionAsync().AsTask(token);

                // got permissions so clear the status string
                rootPage.NotifyUser("", NotifyType.StatusMessage);
            }
#if WINDOWS_APP
            catch (UnauthorizedAccessException)
            {
                rootPage.NotifyUser("Location Permissions disabled by user. Enable access through the settings charm to enable the background task.", NotifyType.StatusMessage);
            }
#endif
            catch (TaskCanceledException)
            {
                rootPage.NotifyUser("Permission check operation canceled.", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
#if WINDOWS_APP
                // If there are no location sensors GetGeopositionAsync()
                // will timeout -- that is acceptable.
                const int WaitTimeoutHResult = unchecked ((int)0x80070102);

                if (ex.HResult == WaitTimeoutHResult) // WAIT_TIMEOUT
                {
                    rootPage.NotifyUser("Operation accessing location sensors timed out. Possibly there are no location sensors.", NotifyType.StatusMessage);
                }
                else
#endif
                {
                    rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
                }
            }
            finally
            {
                cts = null;
            }
        }
示例#14
0
        /// <summary>
        /// Starts the timer to update map objects and the handler to update position
        /// </summary>
        public static async Task InitializeDataUpdate()
        {
            _geolocator = new Geolocator
            {
                DesiredAccuracy         = PositionAccuracy.High,
                DesiredAccuracyInMeters = 5,
                ReportInterval          = 5000,
                MovementThreshold       = 5
            };
            Busy.SetBusy(true, Resources.Translation.GetString("GettingGPSSignal"));
            Geoposition = Geoposition ?? await _geolocator.GetGeopositionAsync();

            GeopositionUpdated?.Invoke(null, Geoposition);
            _geolocator.PositionChanged += (s, e) =>
            {
                Geoposition = e.Position;
                GeopositionUpdated?.Invoke(null, Geoposition);
            };
            _mapUpdateTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(10)
            };
            _mapUpdateTimer.Tick += async(s, e) =>
            {
                if (!UpdateDataMutex.WaitOne(0))
                {
                    return;
                }
                if (_skipNextUpdate)
                {
                    _skipNextUpdate = false;
                }
                else
                {
                    Logger.Write("Updating map");
                    await UpdateMapObjects();
                }

                UpdateDataMutex.ReleaseMutex();
            };
            // Update before starting timer
            Busy.SetBusy(true, Resources.Translation.GetString("GettingUserData"));
            await UpdateMapObjects();
            await UpdateInventory();

            _mapUpdateTimer.Start();
            Busy.SetBusy(false);
        }
        public async void GetCoordinatesWithCallback(Action <string> resolve, Action <string> reject)
        {
            try
            {
                Geolocator geolocator = new Geolocator();
                var        position   = await geolocator.GetGeopositionAsync();

                string result = $"Latitude: {position.Coordinate.Point.Position.Latitude} - Longitude: {position.Coordinate.Point.Position.Longitude}";

                resolve(result);
            }
            catch (Exception e)
            {
                reject(e.Message);
            }
        }
示例#16
0
        public async static Task ReadLocation(bool saveInHistory)
        {
            try
            {
                CurrentPosition = await locator.GetGeopositionAsync().AsTask();

                if (saveInHistory)
                {
                    SaveCurrentLocation();
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }
        }
示例#17
0
        public async Task <BasicGeoposition> GetLocationPoint()
        {
            Geolocator geo = new Geolocator();

            geo.DesiredAccuracy         = PositionAccuracy.High;
            geo.DesiredAccuracyInMeters = 5;

            geo.ReportInterval    = 3000;
            geo.MovementThreshold = 5;

            Geoposition location = await geo.GetGeopositionAsync();

            BasicGeoposition point = location.Coordinate.Point.Position;

            return(point);
        }
示例#18
0
        private async Task <Geoposition> getLocation()
        {
            Geolocator  geolocator  = new Geolocator();
            Geoposition geoposition = null;

            try
            {
                geoposition = await geolocator.GetGeopositionAsync();
            }
            catch (Exception ex)
            {
                // Handle errors like unauthorized access to location
                // services or no Internet access.
            }
            return(geoposition);
        }
        /// <summary>
        /// get my positon
        /// </summary>
        /// <returns></returns>
        private async Task <Geoposition> getCoordinates()
        {
            Geolocator geolocator = new Geolocator();

            geolocator.DesiredAccuracyInMeters = 50;

            try
            {
                return(await geolocator.GetGeopositionAsync(maximumAge : TimeSpan.FromMinutes(5), timeout : TimeSpan.FromSeconds(10)));
            }
            catch (Exception)
            {
                MessageDialog dialog = new MessageDialog("Không thể xác định vị trí của bạn", "Thông báo");
                return(null);
            }
        }
示例#20
0
        private async void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
            geoposition = await geolocator.GetGeopositionAsync();

            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                latTbl.Text  = geoposition.Coordinate.Latitude.ToString();
                longTbl.Text = geoposition.Coordinate.Longitude.ToString();

                getLat  = geoposition.Coordinate.Latitude.ToString();
                getLong = geoposition.Coordinate.Longitude.ToString();

                myMap.Center        = geoposition.Coordinate.Point;
                currentPin.Location = geoposition.Coordinate.Point;
            });
        }
示例#21
0
        public async static Task <Geoposition> GetPosition()
        {
            var accessStatus = await Geolocator.RequestAccessAsync(); //request access

            if (accessStatus != GeolocationAccessStatus.Allowed)
            {
                throw new LocationPermissionException("Access to Location Services Required");
            }

            var geolocator = new Geolocator {
                DesiredAccuracyInMeters = 0
            };                                                               //0 means give what ever you got
            var position = await geolocator.GetGeopositionAsync();

            return(position);
        }
示例#22
0
        public override async void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary <string, object> viewModelState)
        {
            Geolocator locator = new Geolocator();

            Position = await locator.GetGeopositionAsync();

            if (Distance == 0)
            {
                Distance = DistanceOptions.First();
            }

            if (PageSize == 0)
            {
                PageSize = PageSizeOptions.First();
            }
        }
        private async Task <string> GetPosition()
        {
            var g   = new Geolocator();
            var pos = await g.GetGeopositionAsync();

            if (pos != null)
            {
                var loc = await MapLocationFinder.FindLocationsAtAsync(pos.Coordinate.Point);

                if (loc != null && loc.Locations.Count > 0)
                {
                    return(loc.Locations.First().Address.Town);
                }
            }
            return("");
        }
        private async void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Geolocator geolocator = new Geolocator()
            {
                DesiredAccuracyInMeters = 5
            };
            Geoposition pos = await geolocator.GetGeopositionAsync();

            string longitude = pos.Coordinate.Point.Position.Longitude.ToString();
            string latitude  = pos.Coordinate.Point.Position.Latitude.ToString();

            LongBlock.Text = longitude;
            LatBlock.Text  = latitude;

            await TheMap.TrySetViewAsync(pos.Coordinate.Point, 15.0);
        }
        public async Task <Location> GetLocation()
        {
            var geolocator = new Geolocator();

            geolocator.DesiredAccuracyInMeters = 50;

            Geoposition geoposition = await geolocator.GetGeopositionAsync(
                maximumAge : TimeSpan.FromMinutes(5),
                timeout : TimeSpan.FromSeconds(10)
                );

            return(new Location()
            {
                Latitude = geoposition.Coordinate.Latitude, Longitude = geoposition.Coordinate.Longitude
            });
        }
示例#26
0
        public MainPage()
        {
            this.InitializeComponent();
            geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 10;
            geolocator.ReportInterval          = 0;

            myButton.Click += async(sender, e) =>
            {
                geoposition = await geolocator.GetGeopositionAsync();

                string latitude  = geoposition.Coordinate.Latitude.ToString("0.0000000000");
                string Longitude = geoposition.Coordinate.Longitude.ToString("0.0000000000");
                string Accuracy  = geoposition.Coordinate.Accuracy.ToString("0.0000000000");
            };
        }
示例#27
0
        public LocationHelper()
        {
            _geolocator = new Geolocator();
            _geolocator.PositionChanged += (s, e) =>
            {
                this.Position = e.Position;
                if (PositionChanged != null)
                {
                    try { PositionChanged(this.Position); }
                    catch { }
                }
            };

            // initial value
            this.Position = _geolocator.GetGeopositionAsync().AsTask <Geoposition>().Result;
        }
示例#28
0
        public async static void GetGPS(Map myMap)
        {
            Geolocator locator = new Geolocator();

            if (locator.LocationStatus == PositionStatus.Disabled)
            {
                MessageBox.Show("GPS của bạn đang tắt, bất lên để sử dụng tính năng này !!");
            }
            else
            {
                locator.DesiredAccuracyInMeters = 50;
                var mypoint = await locator.GetGeopositionAsync();

                myMap.SetView(new GeoCoordinate(mypoint.Coordinate.Latitude, mypoint.Coordinate.Longitude), 17D);
            }
        }
示例#29
0
        public async Task <Location?> GetLastKnownLocationAsync()
        {
            // no need for permissions as AllowFallbackToConsentlessPositions
            // will allow the device to return a location regardless

            var geolocator = new Geolocator
            {
                DesiredAccuracy = PositionAccuracy.Default,
            };

            geolocator.AllowFallbackToConsentlessPositions();

            var location = await geolocator.GetGeopositionAsync().AsTask();

            return(location?.Coordinate?.ToLocation());
        }
示例#30
0
        private async void MarcarMinhaLocalizacao()
        {
            try
            {
                var locationFinder = new Geolocator
                {
                    DesiredAccuracyInMeters = 50,
                    DesiredAccuracy         = PositionAccuracy.Default,
                    MovementThreshold       = 50
                };

                locationFinder.PositionChanged += LocationFinder_PositionChanged;
                locationFinder.GetGeopositionAsync(maximumAge: TimeSpan.FromSeconds(120), timeout: TimeSpan.FromSeconds(10));
            }
            catch { }
        }