1 (edited by mikejm 2023-05-29 01:16:49)

Topic: Google Autocomplete API to get locations around the user's GPS?

I have been able to implement the Autocomplete example as per this:

https://infinity-code.com/atlas/online- … ample.html

Google's API has a way to limit or focus the results by GPS as this is typically needed:

https://developers.google.com/maps/docu … tocomplete

"You may bias results to a specified circle by passing a location and a radius parameter. Doing so instructs the Places service to prefer showing results within that circle; results outside of the defined area may still be displayed."

I have tried using the following:

OnlineMapsGooglePlacesAutocomplete.Params autoCompleteParams = new OnlineMapsGooglePlacesAutocomplete.Params {
                key = apiKey,
                location = currentLocation, //long,lat as Vector2
                radius = 10 * 1000, //meters (ie. km * 1000)
            };

            OnlineMapsGooglePlacesAutocomplete.Find(searchString, autoCompleteParams).OnComplete += autoCompleteReturned;

However, this does not seem to be working properly. I am not getting reliably biased results to my area. I wonder if there some error in implementation.

For example, if I start typing my home address or a nearby address in, I am still getting very far away results unless I go very far with the typing to almost completely spell it out. Or if I type a chain like Starbucks or McDonald's or a gas station brand, I still find some far away results ahead of close ones. It is better than not giving any location or radius but still not as good as expected.

Can you test this on your end perhaps to confirm if it is working as expected or let me know if there's any easy way to test it objectively against Google's standard?

I also note that "result.description" tends to return addresses of places like this: "McDonald's, Grace Street, City, Province/State, Country." ie. There is no street #. How do we get the full address with street #?

Is there a way to get the result broken down into these components like city, country, street number/name, facility name as separate fields? Also is GPS of the location stored in result too?

Thanks for your help.

Re: Google Autocomplete API to get locations around the user's GPS?

Hi.

I just tested the Autocomplete API with and without location, and it gives different results.
A screenshot is attached.
The location used is Rottweil, Germany.

What do you need to do to check what is wrong:
Add a Log component, and turn on Request Events.
Find the Autocomplete API request in the console, and check it in the browser.
If it's not correct, please let me know what's wrong and I'll make the necessary changes.
If it's correct, unfortunately, I can't help you, because it's a third party service, which I can't affect.

Post's attachments

Attachment icon img1.png 393.91 kb, 25 downloads since 2023-05-29 

Kind Regards,
Infinity Code Team.

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

3 (edited by mikejm 2023-06-07 05:52:49)

Re: Google Autocomplete API to get locations around the user's GPS?

Alex Vertax wrote:

Hi.

I just tested the Autocomplete API with and without location, and it gives different results.
A screenshot is attached.
The location used is Rottweil, Germany.

What do you need to do to check what is wrong:
Add a Log component, and turn on Request Events.
Find the Autocomplete API request in the console, and check it in the browser.
If it's not correct, please let me know what's wrong and I'll make the necessary changes.
If it's correct, unfortunately, I can't help you, because it's a third party service, which I can't affect.

Thanks Alex. I think I figured at least one part out. Google is designed to be inefficient to make you make multiple queries to get anything useful.

https://stackoverflow.com/questions/598 … et-numbers

According to that we must take the placeID from each autocomplete suggestion and then do a Places Details query for each to get the actual addresses. Just a way for them to charge 5 times more for each result.

http://infinity-code.com/doxygen/online … tails.html

4 (edited by mikejm 2023-06-07 06:12:29)

Re: Google Autocomplete API to get locations around the user's GPS?

I see Bing Autocomplete solves this silly issue by providing full address details for all queries:

https://learn.microsoft.com/en-us/bingm … utosuggest

Would it be possible to add that to the API? This looks way way way better than Google's service. It has more structured address responses with street, country, postal code, etc. all separately defined. Google also requests you to put their logo on anything using their service which is just ugly.

Only thing Bing Autosuggest doesn't give automatically is GPS, which I believe we must then get with Location search:

https://learn.microsoft.com/en-us/bingm … by-address
http://infinity-code.com/doxygen/online … 08b7c528d6

Thanks for any help.

Re: Google Autocomplete API to get locations around the user's GPS?

The current version of Online Maps does not have an API for this service, but that does not mean that you cannot use it.
Make a request manually and process the response.
Something like:

using UnityEngine;

namespace InfinityCode.OnlineMapsSupport
{
    public class BingMapsAutocompleteExample : MonoBehaviour
    {
        private void Start()
        {
            string key = OnlineMapsKeyManager.BingMaps();
            string query = "El bur";
            
            // This is wrong because you have to wait for the GPS to initialize,
            // and I did it here to minimize the complexity of the example.
            Vector2 userLocation = OnlineMapsLocationService.instance.position;
            
            string url = $"https://dev.virtualearth.net/REST/v1/Autosuggest?query={OnlineMapsWWW.EscapeURL(query)}&userLocation={userLocation.y},{userLocation.x},5&includeEntityTypes=Business&key={key}";
            OnlineMapsWWW www = new OnlineMapsWWW(url);
            www.OnComplete += OnRequestComplete;
        }
        
        private void OnRequestComplete(OnlineMapsWWW www)
        {
            if (www.hasError)
            {
                Debug.LogError(www.error);
                return;
            }

            OnlineMapsJSONItem json = OnlineMapsJSON.Parse(www.text);
            foreach (OnlineMapsJSONValue item in json["//formattedAddress"])
            {
                Debug.Log(item.V<string>());
            }
        }
    }
}
Kind Regards,
Infinity Code Team.

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