Topic: Google Places Autocomplete returns last value

Hi

I'm using the AutocompleteApi from google,example from Atlas of examples.

The problem is that when i search a place, it always returns the last placa i searched, even after restarting editor in play mode.

This is the code i'm using based on the example:

public class AutocompleteAPIsearch : MonoBehaviour
{
    /// <summary>
    /// Google API Key
    /// </summary>
    public string apiKey;
    private string placestring, types, language, components;
    Vector2 lnglat;
    int offset, radius;

    public InputField inputfield;

    public GameObject placeButton, transformada;

    OnlineMapsGooglePlacesAutocompleteResult[] results;

    public void autocomplete()
    {



        placestring = inputfield.text;
        offset = -1;
        types = null;
        lnglat = default(Vector2);
        radius = -1;

        if (LeanLocalization.CurrentLanguage == "Portuguese") language = "pt";
        if (LeanLocalization.CurrentLanguage == "English") language = "en";


        components = "country:" + language;
        Debug.Log(components);

        //lnglat = new Vector2(OnlineMapsLocationService.instance.position.x, OnlineMapsLocationService.instance.position.y);

        // Makes a request to Google Places Autocomplete API. See the reference in OnlineMaps Api Reference
        OnlineMapsGooglePlacesAutocomplete.Find(
            placestring,
            apiKey,
            types,
            offset,
           lnglat,
            radius,
            language,
            components
            ).OnComplete += OnComplete;
    }

    /// <summary>
    /// This method is called when a response is received.
    /// </summary>
    /// <param name="s">Response string</param>
    private void OnComplete(string s)
    {
        // Trying to get an array of results.
        results = OnlineMapsGooglePlacesAutocomplete.GetResults(s);

        //results = results.Distinct().ToArray();
        // If there is no result
        if (results == null)
        {
            Debug.Log("Error");
            Debug.Log(s);
            return;
        }
        else
        {

            // Log description of each result.
            // foreach (OnlineMapsGooglePlacesAutocompleteResult result in results)
            for (int i = 0; i < results.Length; i++)
            {
                Instantiate(placeButton, transformada.transform);

                placeButton.GetComponentInChildren<Text>().text = results[i].description;

                Debug.Log(results[i].description);
            }

        }
    }

I always get the last place, but the strangest thing is that in the debug console, the name of the place is correct, and doesnt show the last place name.

Post's attachments

Attachment icon Capturar.PNG 175.07 kb, 79 downloads since 2018-01-24 

Re: Google Places Autocomplete returns last value

Hello.

In your code, I see how you create instances for the result of the current request.
But where do you destroy the instances of the previous request?

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Google Places Autocomplete returns last value

Alex Vertax wrote:

Hello.

In your code, I see how you create instances for the result of the current request.
But where do you destroy the instances of the previous request?


I have that in other script:

 public void RemoveOldSearches()
    {
        InputSearchPlaceText.text = ""; 

        foreach (Transform child in ContentTransform.transform)
        {
            Destroy(child.gameObject);
        }

    }

and a in other script in a void update:

      if (InputSearchPlaceText.text == "") //Se o utilizador apagar a string toda entao apaga todos os butoes do searchPanel
        {
            foreach (Transform child in ContentTransform.transform)  //destroi todos os clones dos butoes da search antiga
            {
                Destroy(child.gameObject);
            }
        }

What do you think is happening?

Re: Google Places Autocomplete returns last value

It needs to be tested.
If you send your scene as a package to support (support@infinity-code.com) I'll check it and say you exactly what's wrong.
Otherwise, you need to test it yourself.
Add Log into each method, and make sure everything works as planned.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.