Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to display the address to formal format

Xammy10Xammy10 Member ✭✭✭
edited March 2018 in Xamarin.Android

I'm finding a way to remove unnecessary characters here:

but every codes i found is for java
like this one:
https://stackoverflow.com/questions/12673357/how-to-get-only-city-state-country-from-lat-long-in-android

This is my code on getting the Locality, postalcode and country name:

public void AddressDisplay(Address address)
{
if (address != null)
{
StringBuilder deviceAddress = new StringBuilder();
for (int i = 0; i > address.MaxAddressLineIndex; i++)
{
deviceAddress.AppendLine(address.Locality).Append("\n");
deviceAddress.AppendLine(address.PostalCode).Append("\n");
deviceAddress.AppendLine(address.CountryName).Append("\n");
}

            Toast.MakeText(this, deviceAddress.ToString(),ToastLength.Short).Show();
            AddressView.Text = deviceAddress.ToString();
        }
        else
        {
            AddressView.Text = "Current Address Not Available, Please Try Again Later...";
        }
    }

still can't make it to the format that i want, and the address are not showing anymore.

I'm not sure if this is my way is right, I'm just trying to convert the codes from jave.

can you tell me how to do it on C#?

Thanks in advance :)

Best Answer

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited March 2018 Accepted Answer

    Hello all,
    @Enohara

    IList<Android.Location.Address> addresses = geocoder.GetFromLocation(lat , long, 1); if (addresses != null && addresses.Count > 0) { string theAddress = addresses[0].GetAddressLine(0); string theCity = addresses[0].Locality; string theState = addresses[0].AdminArea; string theCountry = addresses[0].CountryName; string thePostalCode = addresses[0].PostalCode; string theKnownName = addresses[0].FeatureName; }
    no tested but it should working

Answers

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    edited March 2018
    You have the structured data after AddressLines array. It's a fairly straightforward bit of C# coding to parse this. Maybe using a regex.
  • Xammy10Xammy10 Member ✭✭✭

    @JamesLavery said:
    You have the structured data after AddressLines array. It's a fairly straightforward bit of C# coding to parse this. Maybe using a regex.

    Can you send some samples or links about regex?

    Thanks in advance :)

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello all,
    @Enohara try to deserialize the string your receive in address object as this
    Android.Locations.Address => Address contains => all informations

    https://developer.android.com/reference/android/location/Address.html

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    @Enohara I'm going to be maybe a bit harsh here. Just do your own research on regex etc. You can't expect people to do it for you.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited March 2018 Accepted Answer

    Hello all,
    @Enohara

    IList<Android.Location.Address> addresses = geocoder.GetFromLocation(lat , long, 1); if (addresses != null && addresses.Count > 0) { string theAddress = addresses[0].GetAddressLine(0); string theCity = addresses[0].Locality; string theState = addresses[0].AdminArea; string theCountry = addresses[0].CountryName; string thePostalCode = addresses[0].PostalCode; string theKnownName = addresses[0].FeatureName; }
    no tested but it should working

  • Xammy10Xammy10 Member ✭✭✭
    edited March 2018

    @JamesLavery said:
    @Enohara I'm going to be maybe a bit harsh here. Just do your own research on regex etc. You can't expect people to do it for you.

    It's ok sir, I just want some Idea from the person who already know it, but still, Thank you very much :)

Sign In or Register to comment.