Ejemplo n.º 1
0
        /// <summary>
        /// Event handler called when the user tap and hold in the map
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Event arguments</param>
        private async void OnMapHold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ReverseGeocodeQuery query;
            List <MapLocation>  mapLocations;
            string      pushpinContent;
            MapLocation mapLocation;

            query = new ReverseGeocodeQuery();
            query.GeoCoordinate = this.Map.ConvertViewportPointToGeoCoordinate(e.GetPosition(this.Map));

            mapLocations = (List <MapLocation>) await query.GetMapLocationsAsync();

            mapLocation = mapLocations.FirstOrDefault();

            if (mapLocation != null)
            {
                this.RouteDirectionsPushPin.GeoCoordinate = mapLocation.GeoCoordinate;

                pushpinContent = mapLocation.Information.Name;
                pushpinContent = string.IsNullOrEmpty(pushpinContent) ? mapLocation.Information.Description : null;
                pushpinContent = string.IsNullOrEmpty(pushpinContent) ? string.Format("{0} {1}", mapLocation.Information.Address.Street, mapLocation.Information.Address.City) : null;

                this.RouteDirectionsPushPin.Content    = pushpinContent.Trim();
                this.RouteDirectionsPushPin.Visibility = Visibility.Visible;
            }
        }
Ejemplo n.º 2
0
        public static async Task GetAddress(double latitude, double longtitude)
        {
            var locationAddress = new LocationAddress();
            try
            {
                var myReverseGeocodeQuery = new ReverseGeocodeQuery
                    {
                        GeoCoordinate = new GeoCoordinate(latitude, longtitude)
                    };
                IList<MapLocation> locations = await myReverseGeocodeQuery.GetMapLocationsAsync();

                if (locations.Count > 0)
                {
                    var address = locations.First().Information.Address;
                    locationAddress = new LocationAddress
                                          {
                                              Street = address.Street,
                                              HouseNumber = address.HouseNumber,
                                              PostalCode = address.PostalCode,
                                              City = address.City,
                                              District = address.District,
                                              State = address.State,
                                              Country = address.Country
                                          };
                }
            }
            catch (Exception ex)
            {
                BugSenseHandler.Instance.LogException(ex);
            }

            App.ViewModel.CurrentAddress = locationAddress;
        }
        private async void mapControl_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            status.Text = "querying for address...";

            var point      = e.GetPosition(mapControl);
            var coordinate = mapControl.ConvertViewportPointToGeoCoordinate(point);

            var pushpin = new Pushpin
            {
                GeoCoordinate = coordinate,
                Content       = ++pinNumber,
            };

            MapExtensions.GetChildren(mapControl).Add(pushpin);

            position.Text = string.Format("Latitude: {0}\nLongitude: {1}\n",
                                          FormatCoordinate(coordinate.Latitude, 'N', 'S'),
                                          FormatCoordinate(coordinate.Longitude, 'E', 'W'));

            ReverseGeocodeQuery query = new ReverseGeocodeQuery {
                GeoCoordinate = coordinate
            };
            IList <MapLocation> results = await query.GetMapLocationsAsync();

            position.Text += string.Format("{0} locations found.\n", results.Count);
            MapLocation location = results.FirstOrDefault();

            if (location != null)
            {
                position.Text += FormatAddress(location.Information.Address);
            }
            status.Text += "complete";
        }
Ejemplo n.º 4
0
        public static async Task <string> GetAddressAsync(Geoposition position)
        {
            var reverseGeocodeQuery = new ReverseGeocodeQuery();

            reverseGeocodeQuery.GeoCoordinate = position.Coordinate.ToGeoCoordinate();
            var locations = await reverseGeocodeQuery.GetMapLocationsAsync();

            if (locations != null && locations.Count > 0)
            {
                var location = locations.FirstOrDefault();

                if (location.Information != null && location.Information.Address != null)
                {
                    var address = location.Information.Address;

                    string streetAddress  = string.Format("{0} {1}", address.Street, address.HouseNumber).Trim();
                    string cityAddress    = string.Format("{0} {1}", address.PostalCode, address.City).Trim();
                    string countryAddress = address.Country;

                    return(string.Format("{0}, {1}, {2}", streetAddress, cityAddress, countryAddress)
                           .Trim(new char[] { ' ', ',' }));
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        public async void GetPhoneLocation()
        {
            try
            {
                _geolocator.DesiredAccuracyInMeters = 10;
                //_geolocator.DesiredAccuracy = PositionAccuracy.High;
                Geoposition geoposition = null;

                try
                {
                    geoposition = await _geolocator.GetGeopositionAsync(maximumAge : TimeSpan.FromMinutes(5), timeout : TimeSpan.FromSeconds(10));
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                ReverseGeocodeQuery myReverseGeocodeQuery = new ReverseGeocodeQuery();
                myReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
                IList <MapLocation> locations = await myReverseGeocodeQuery.GetMapLocationsAsync();

                if (LocationRetrieved != null)
                {
                    LocationRetrieved(this, locations.FirstOrDefault());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private async void GetLocation_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                //Get the current position
                var geolocator = new Geolocator() { DesiredAccuracyInMeters = 10 };

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

                //Perform the reverse geocode query 
                var query = new ReverseGeocodeQuery() { GeoCoordinate = geoposition.Coordinate.ToGeoCoordinate() };
                var geoCodeResults = await query.GetMapLocationsAsync();
                var address = geoCodeResults.First().Information.Address;

                //Print all 
                Coordinates.Text = FormatCoordinates(geoposition);
                Address.Text = FormatAddress(address);

            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    //Location services turned off, ask user to turn it on.
                    AskUserToTurnOnLocationServices();
                }
                else
                {
                    MessageBox.Show("Unable to get location");
                }
            }
        }
Ejemplo n.º 7
0
        private async void SaveTheCar()
        {
            Car.Geo = UserCurrentPosition;

            this.OnUiThread(() =>
            {
                CarPosition       = UserCurrentPosition;
                PushpinVisibility = "Visible";
            });

            // Perform the reverse geocode query
            var query = new ReverseGeocodeQuery()
            {
                GeoCoordinate = UserCurrentPosition
            };
            var geoCodeResults = await query.GetMapLocationsAsync();

            var address = geoCodeResults.First().Information.Address;

            Car.Address = FormatAddress(address);

            IsolatedStorageSettings.ApplicationSettings["car"] = Car;
            IsolatedStorageSettings.ApplicationSettings.Save();
            updateLiveTile();
            Messenger.Default.Send <String>("CarPositionChanged");
        }
Ejemplo n.º 8
0
        public async void GetPhoneLocation()
        {
            try
            {
                _geolocator.DesiredAccuracyInMeters = 10;
                //_geolocator.DesiredAccuracy = PositionAccuracy.High;
                Geoposition geoposition = null;

                try
                {
                    geoposition = await _geolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10));
                }
                catch (Exception ex)
                {

                    throw ex;
                }
                
                ReverseGeocodeQuery myReverseGeocodeQuery = new ReverseGeocodeQuery();
                myReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
                IList<MapLocation> locations = await myReverseGeocodeQuery.GetMapLocationsAsync();
                if (LocationRetrieved != null)
                {
                    LocationRetrieved(this, locations.FirstOrDefault());
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 9
0
        private async void GetLocation_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                //Get the current position
                var geolocator = new Geolocator()
                {
                    DesiredAccuracyInMeters = 10
                };

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

                //Perform the reverse geocode query
                var query = new ReverseGeocodeQuery()
                {
                    GeoCoordinate = geoposition.Coordinate.ToGeoCoordinate()
                };
                var geoCodeResults = await query.GetMapLocationsAsync();

                var address = geoCodeResults.First().Information.Address;

                //Print all
                Coordinates.Text = FormatCoordinates(geoposition);
                Address.Text     = FormatAddress(address);
            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    //Location services turned off, ask user to turn it on.
                    AskUserToTurnOnLocationServices();
                }
                else
                {
                    MessageBox.Show("Unable to get location");
                }
            }
        }
Ejemplo n.º 10
0
        public static async Task AddLocation(Microsoft.Phone.Maps.Controls.Map myMap, Microsoft.Phone.Controls.PhoneTextBox myTextBox, System.Windows.Input.GestureEventArgs e, double latitude, double longitude, bool completeAddress = false, string pinContent = "")
        {
            ReverseGeocodeQuery query;
            List<MapLocation> mapLocations;
            string pushpinContent = "";
            MapLocation mapLocation;

            query = new ReverseGeocodeQuery();
            if (e != null)
                query.GeoCoordinate = myMap.ConvertViewportPointToGeoCoordinate(e.GetPosition(myMap));
            else
                query.GeoCoordinate = new GeoCoordinate(latitude, longitude);

            mapLocations = (List<MapLocation>)await query.GetMapLocationsAsync();
            mapLocation = mapLocations.FirstOrDefault();

            if (mapLocation != null && !String.IsNullOrEmpty(mapLocation.Information.Address.Country))
            {
                MapLayer pinLayout = new MapLayer();
                Pushpin MyPushpin = new Pushpin();
                MapOverlay pinOverlay = new MapOverlay();
                if (myMap.Layers.Count > 0)
                {
                    myMap.Layers.RemoveAt(myMap.Layers.Count - 1);
                }

                myMap.Layers.Add(pinLayout);

                MyPushpin.GeoCoordinate = mapLocation.GeoCoordinate;


                pinOverlay.Content = MyPushpin;
                pinOverlay.GeoCoordinate = mapLocation.GeoCoordinate;
                pinOverlay.PositionOrigin = new Point(0, 1);
                pinLayout.Add(pinOverlay);

                if (!completeAddress)
                    pushpinContent = getAddress(mapLocation);
                else
                    pushpinContent = getCompleteAddress(mapLocation);

                if (!string.IsNullOrEmpty(pinContent))
                    pushpinContent = pinContent;

                MyPushpin.Content = pushpinContent.Trim();
                if (myTextBox != null)
                    myTextBox.Text = MyPushpin.Content.ToString();
            }
        }
Ejemplo n.º 11
0
        public static async Task AddLocation(Microsoft.Phone.Maps.Controls.Map myMap, Microsoft.Phone.Controls.PhoneTextBox myTextBox, System.Windows.Input.GestureEventArgs e, double latitude, double longitude, bool completeAddress = false, string pinContent = "")
        {
            ReverseGeocodeQuery query;
            List <MapLocation>  mapLocations;
            string      pushpinContent = "";
            MapLocation mapLocation;

            query = new ReverseGeocodeQuery();
            if (e != null)
            {
                query.GeoCoordinate = myMap.ConvertViewportPointToGeoCoordinate(e.GetPosition(myMap));
            }
            else
            {
                query.GeoCoordinate = new GeoCoordinate(latitude, longitude);
            }

            mapLocations = (List <MapLocation>) await query.GetMapLocationsAsync();

            mapLocation = mapLocations.FirstOrDefault();

            if (mapLocation != null && !String.IsNullOrEmpty(mapLocation.Information.Address.Country))
            {
                MapLayer   pinLayout  = new MapLayer();
                Pushpin    MyPushpin  = new Pushpin();
                MapOverlay pinOverlay = new MapOverlay();
                if (myMap.Layers.Count > 0)
                {
                    myMap.Layers.RemoveAt(myMap.Layers.Count - 1);
                }

                myMap.Layers.Add(pinLayout);

                MyPushpin.GeoCoordinate = mapLocation.GeoCoordinate;


                pinOverlay.Content        = MyPushpin;
                pinOverlay.GeoCoordinate  = mapLocation.GeoCoordinate;
                pinOverlay.PositionOrigin = new Point(0, 1);
                pinLayout.Add(pinOverlay);

                if (!completeAddress)
                {
                    pushpinContent = getAddress(mapLocation);
                }
                else
                {
                    pushpinContent = getCompleteAddress(mapLocation);
                }

                if (!string.IsNullOrEmpty(pinContent))
                {
                    pushpinContent = pinContent;
                }

                MyPushpin.Content = pushpinContent.Trim();
                if (myTextBox != null)
                {
                    myTextBox.Text = MyPushpin.Content.ToString();
                }
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Event handler called when the user tap and hold in the map
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Event arguments</param>
        private async void OnMapHold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ReverseGeocodeQuery query;
            List<MapLocation> mapLocations;
            string pushpinContent;
            MapLocation mapLocation;

            query = new ReverseGeocodeQuery();
            query.GeoCoordinate = this.Map.ConvertViewportPointToGeoCoordinate(e.GetPosition(this.Map));

            mapLocations = (List<MapLocation>)await query.GetMapLocationsAsync();
            mapLocation = mapLocations.FirstOrDefault();

            if (mapLocation != null)
            {
                this.RouteDirectionsPushPin.GeoCoordinate = mapLocation.GeoCoordinate;

                pushpinContent = mapLocation.Information.Name;
                pushpinContent = string.IsNullOrEmpty(pushpinContent) ? mapLocation.Information.Description : null;
                pushpinContent = string.IsNullOrEmpty(pushpinContent) ? string.Format("{0} {1}", mapLocation.Information.Address.Street, mapLocation.Information.Address.City) : null;

                this.RouteDirectionsPushPin.Content = pushpinContent.Trim();
                this.RouteDirectionsPushPin.Visibility = Visibility.Visible;
            }
        }
        async void locationWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                LocationIndicator.IsRunning = true;
                TxtBoxLocation.IsEnabled    = false;
                SetScreenButtonsEnabled(false);
            });

            if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.None)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    OSHelper.ShowToast(AppResources.TextID17);

                    LocationIndicator.IsRunning = false;
                    TxtBoxLocation.IsEnabled    = true;
                    SetScreenButtonsEnabled(true);
                });
                return;
            }

            var currentPosition = await GetCurrentLocation();

            if (currentPosition != null)
            {
                //string reverseGeocodingQuery = String.Format(Constant.GOOGLE_REVERSE_GEOCODING_URI, currentPosition.Latitude, currentPosition.Longitude);
                //HttpClient client = new HttpClient()
                //{
                //    Timeout = TimeSpan.FromMilliseconds(5000)
                //};
                //HttpRequestMessage req = new HttpRequestMessage()
                //{
                //    RequestUri = new Uri(reverseGeocodingQuery, UriKind.Absolute)
                //};
                //var res = await client.SendAsync(req);
                //string content = await res.Content.ReadAsStringAsync();
                //string address = ParseGoogleResponse(content);

                Dispatcher.BeginInvoke(async() =>
                {
                    ReverseGeocodeQuery reverseGeocodingQuery = new ReverseGeocodeQuery()
                    {
                        GeoCoordinate = currentPosition
                    };
                    var results = await reverseGeocodingQuery.GetMapLocationsAsync();

                    LocationIndicator.IsRunning = false;
                    TxtBoxLocation.IsEnabled    = true;
                    SetScreenButtonsEnabled(true);

                    if (results.Count > 0)
                    {
                        var geoAddress      = results[0].Information.Address;
                        TxtBoxLocation.Text = FormatAddress(geoAddress);
                    }
                });
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    LocationIndicator.IsRunning = false;
                    TxtBoxLocation.IsEnabled    = true;
                    SetScreenButtonsEnabled(true);
                });
            }
        }
      private async void mapControl_Hold(object sender, System.Windows.Input.GestureEventArgs e)
      {
         status.Text = "querying for address...";

         var point = e.GetPosition(mapControl);
         var coordinate = mapControl.ConvertViewportPointToGeoCoordinate(point);

         var pushpin = new Pushpin
         {
            GeoCoordinate = coordinate,
            Content = ++pinNumber,
         };
         MapExtensions.GetChildren(mapControl).Add(pushpin);

         position.Text = string.Format("Latitude: {0}\nLongitude: {1}\n",
             FormatCoordinate(coordinate.Latitude, 'N', 'S'),
             FormatCoordinate(coordinate.Longitude, 'E', 'W'));

         ReverseGeocodeQuery query = new ReverseGeocodeQuery { GeoCoordinate = coordinate };
         IList<MapLocation> results = await query.GetMapLocationsAsync();
         position.Text += string.Format("{0} locations found.\n", results.Count);
         MapLocation location = results.FirstOrDefault();
         if (location != null)
         {
            position.Text += FormatAddress(location.Information.Address);
         }
         status.Text += "complete";
      }