Crerem
Crerem

Reputation: 1359

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

Upvotes: 7

Views: 3077

Answers (1)

Baklap4
Baklap4

Reputation: 4202

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.

Upvotes: 3

Related Questions