ReverseGeocodeAsync() public method

public ReverseGeocodeAsync ( double latitude, double longitude ) : Task>
latitude double
longitude double
return Task>
        private async void InitLocation()
        {
            try
            {
                var locator = CrossGeolocator.Current;

                if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
                {
                    var locatedPosition = await locator.GetPositionAsync(timeout : TimeSpan.FromSeconds(5000));

                    Latitude  = locatedPosition.Latitude;
                    Longitude = locatedPosition.Longitude;
                }
                else
                {
                    Message = "Location not available";
                }
            }
            catch (Exception ex)
            {
                Message = "A problem occure while locating your position.";
            }


            try
            {
                if (CrossConnectivity.Current.IsConnected)
                {
                    var geocoder = new GEOGoogle.GoogleGeocoder();

                    var addresses = await geocoder.ReverseGeocodeAsync(Latitude, Longitude);

                    var approximateAddress = addresses.FirstOrDefault();

                    if (approximateAddress != null)
                    {
                        Address = approximateAddress.FormattedAddress;
                    }
                    else
                    {
                        Address = "Address not found please enter manually";
                    }
                }
                else
                {
                    Message = "Please connect to internet to locate your location address.";
                }
            }
            catch (Exception ex)
            {
                Message = "An error occured while connecting to end point.";
            }
        }
Example #2
0
        //public  void OnClearClicked(object sender,EventArgs e)
        //{
        //    CrossSettings.Current.Clear();
        //}
        private async void InitMap()
        {
            var position = new Position();

            var dialog = UserDialogs.Instance.Loading("Loading Map...");

            try
            {
                dialog.Title = "Locating your position";
                var locator = CrossGeolocator.Current;

                if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
                {
                    var locatedPosition = await locator.GetPositionAsync(timeout : TimeSpan.FromSeconds(10000));

                    // For Test Purpose Only
                    //34.78680923/72.35325037/
                    // var mylat = 34.78680923d;
                    // var mylong = 72.35325037d;

                    position = new Position(locatedPosition.Latitude, locatedPosition.Longitude);

                    /// For Test Purpos Only
                    //  position = new Position(mylat, mylong);


                    MechMap = new Utils.MechMap(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(1)))
                    {
                        IsShowingUser = true,
                        MapType       = MapType.Street
                    };


                    MechMap.HorizontalOptions = LayoutOptions.Fill;
                    MechMap.VerticalOptions   = LayoutOptions.Fill;
                    MapGrid.Children.Add(MechMap);
                    MechMap.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(1)));
                }
                else
                {
                    AddressesText.Text = "Location not available";
                }
            }
            catch (Exception ex)
            {
                AddressesText.Text = "GPS is unable to locate your location.";
            }

            try
            {
                dialog.Title = "Searching Mechanics...";
                if (CrossConnectivity.Current.IsConnected)
                {
                    var client = new HttpClient();
                    var server = new Server();

                    var EndPoint = string.Format(server.NearestMechanicEndPoint, position.Latitude, position.Longitude);

                    var response = await client.GetAsync(EndPoint);

                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsStringAsync();

                        var list = JsonConvert.DeserializeObject <List <Mechanic> >(content);

                        Mechanics = new ObservableCollection <Mechanic>(list);


                        foreach (var mech in Mechanics)
                        {
                            Pin pin = new Pin()
                            {
                                Position = new Position(mech.Latitude, mech.Longitude),
                                Address  = mech.Address,
                                Label    = $"{mech.Name}, {mech.Title}",
                                Type     = PinType.Place
                            };
                            pin.BindingContext = mech;
                            pin.Clicked       += Pin_Clicked;

                            if (MechMap != null)
                            {
                                MechMap.Pins.Add(pin);
                            }
                        }
                    }
                    else
                    {
                        await DisplayAlert("Mech Server", "Service not available", "OK");
                    }
                }
                else
                {
                    dialog.Title = "Connection Problem";
                    await DisplayAlert("Internet", "Please connect to internet", "OK");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Mech Server", "Unable to connect server", "OK");
            }

            try
            {
                dialog.Title = "Searching your address";
                if (CrossConnectivity.Current.IsConnected)
                {
                    var geocoder = new GEOGoogle.GoogleGeocoder();

                    var addresses = await geocoder.ReverseGeocodeAsync(position.Latitude, position.Longitude);

                    var approximateAddress = addresses.FirstOrDefault();

                    if (approximateAddress != null)
                    {
                        this.AddressesText.Text = approximateAddress.FormattedAddress;
                    }
                    else
                    {
                        this.AddressesText.Text = "Address not found for this location";
                    }
                }
                else
                {
                    this.AddressesText.Text = "Please connect to internet to locate your location address.";
                }
            }
            catch (Exception ex)
            {
                this.AddressesText.Text = "Address not found.";
            }

            dialog.Dispose();
        }