A.M.Roomi
A.M.Roomi

Reputation: 123

Google Places Autocomplete is not working Sometimes in Xamarin Android Project

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));
        }
    }
}

Upvotes: 3

Views: 1094

Answers (1)

evan
evan

Reputation: 5701

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!

Upvotes: 2

Related Questions