Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Autocomplete language

I have the following situation. On a website users are submitting location of apartments based on google Autocomplete . They pick a city from google suggestion list and i take that city and country (via api) and save them to the database.

Other users may search for listings in that city using also a google Autocomplete form. The problem appears when the users are using different languages.

For ex - user 1 is in USA. When he submits his listing i receive from google the country "united states" .

When user 2 (who lives in Netherlands) search using google Autocomplete i receive the country "Verenigde Staten" (USA in duch). But the search algorithm will not function since the submitted listing is saved as "united states" and not "Verenigde Staten".

Is there a way to show the suggestion list in user language but receive the results in English (so i can save and search using only English). Maybe translate the results ?

I know i can "block" the suggestion list in english but i would like to use this as final measure.

This is my code

input = (document.getElementById('property_city_front'));
defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-90, -180),
    new google.maps.LatLng(90, 180)
);

options = {
    bounds: defaultBounds,
    types: ['(cities)'],
};

componentForm = {
    establishment: 'long_name',
    street_number: 'short_name',
    route: 'long_name',
    locality: 'long_name',
    administrative_area_level_1: 'long_name',
    administrative_area_level_2: 'long_name',
    country: 'long_name',
    postal_code: 'short_name',
    postal_code_prefix: 'short_name',
    neighborhood: 'long_name',

};


if (document.getElementById('property_city_front')) {
    autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        place = autocomplete.getPlace();
        fillInAddress(place);
    });
}

Following the suggestion i retrive the place id(auto complete made in Dutch) and query the PlacesService but the results i got are still in Dutch and not in english. Here is the code

  var request = {
        placeId: 'ChIJOwg_06VPwokRYv534QaPC8g',
        id: '7eae6a016a9c6f58e2044573fb8f14227b6e1f96',
        language:'en'
    };

    map = new google.maps.Map(document.getElementById('searchmap'), {
    zoom: 15,
    scrollwheel: false
    });

    service = new google.maps.places.PlacesService(map);
    service.getDetails(request, callback);

    function callback(place, status) {
        console.log(status);
        console.log (place);
    }
like image 324
Crerem Avatar asked Nov 13 '15 10:11

Crerem


People also ask

How do I use Google autocomplete?

Click Search features from the menu on the left and then click the Autocomplete tab. Click on the slider to set Enable autocomplete to On. It can take up to 2-4 days for autocompletions tailored to your search engine to start appearing.

How do I use Google Places autocomplete API?

Ensure that the API key(s) used for all Place Autocomplete and Place Details requests within a session belong to the same Cloud Console project. Be sure to pass a unique session token for each new session. Using the same token for more than one session will result in each request being billed individually.


1 Answers

This question is most likely already answered here.

In short the response contains an ID which should be the same for both the queries. You can use this id to identify what country the user is looking for. According to the documentation:

id contains a unique stable identifier denoting this place. This identifier may not be used to retrieve information about this place, but can be used to consolidate data about this Place, and to verify the identity of a Place across separate searches. As ids can occasionally change, it's recommended that the stored id for a Place be compared with the id returned in later Details requests for the same Place, and updated if necessary.

You can query the Places API using the reference. Reference is different per language. You could optional add the retrieved ID.

like image 196
Baklap4 Avatar answered Oct 17 '22 10:10

Baklap4