3

I am implementing Google Places Autocomplete in my Xamarin Android project. (This is not a Xamarin Forms Project). When I search first time, the autocomplete works properly but consequent searches do not work properly.

enter image description here

Even if the autocomplete works, if I select an item, it will not call OnActivityResult function and get back the above screen again.

Even these posts did not solve my problem

I have not set a billing account but I dont think that is the problem because it works sometimes. How can I solve this issue?

(Please note this is not a duplicate question since I could not find any questions like this)

This is the code I am using to open Google Places Autocomplete view

private void BtnLocationOnClick(object sender, EventArgs e)
{
    List<Place.Field> fields = new List<Place.Field>();

    fields.Add(Place.Field.Id);
    fields.Add(Place.Field.Name);
    fields.Add(Place.Field.LatLng);
    fields.Add(Place.Field.Address);

    Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.Overlay, fields)
        .SetCountry("US")
        .Build(this);

    StartActivityForResult(intent, 1);
}

Once the item is selected OnActivityResult function need to be called

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    if (requestCode == 1)
    {
        if (resultCode == Android.App.Result.Ok)
        {
            Place place = Autocomplete.GetPlaceFromIntent(data);

            txtLocation.Text = place.Name;

            googleMap.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(place.LatLng, 15));
        }
    }
}
evan
  • 5,443
  • 2
  • 11
  • 20
A.M.Roomi
  • 113
  • 2
  • 10
  • Could you add some code and see how you are implementing it? – Juanes30 Jan 25 '20 at 06:03
  • Hi @Juanes30 I have added the code and updated the question – A.M.Roomi Jan 25 '20 at 06:18
  • I faced the same situation you explained above. But here's a tutorial I followed and it worked for me. I was using Xamarin.googleservices.places and I realized later that it was deprecated and used the same lib you're using above Xamarin.Google.Android.Places. Kindly check that Url: https://www.youtube.com/watch?v=n9WHrc3ffYY – Peter Maher Jan 26 '20 at 19:49
  • I also have the same problem. – Vishva Vijay Apr 12 '20 at 15:14

1 Answers1

2

Enabling billing on your project is mandatory for the Places API (or any other Maps API) to work. Otherwise you are limited to 1 request per day.

Follow Google's get started guide to create a billing account and link it to your project.

Also make sure that the Places API is enabled on your project too.

Hope this helps!

evan
  • 5,443
  • 2
  • 11
  • 20