Search code examples
javascriptgoogle-places-autocompletegeocomplete

How can I obtain search results in English with Google Place Geocomplete?


My problem is:

Place names are written in different ways depending on the Geolocation, i.e. "Majorca" in Spanish is "Mallorca".

Is there a way to codify the results of a search so that, if the user types "Mallorca" (in their own language) in the search box, the results automatically relate to the English equivalent "Majorca"? Any suggestion?

Thanks!


Solution

  • I found a solution, if anyone need, I've did it in this way:

    1. Get de PLACE ID
    2. Then call api url with PLACE ID value

                  $("myInput").geocomplete({
                  map: ".map_canvas",
                  details: "form ",
                  detailsAttribute: "data-geo",
                  location: center
              }).bind("geocode:result", function(event, result){
                  var locAPI = "https://maps.googleapis.com/maps/api/geocode/json?key=[MY API KEY]&language=en&place_id="+result.place_id;
      
                  $.ajax({
                      type: "GET", 
                      url: locAPI,
                      success: function(data){    
                      data.results[0].address_components.forEach(function(item, index) {
                              item.types.forEach(function(type, index) {
                                  if (type === 'country') {
                                      $("#MYCOUNTRYFIELD").val(item.long_name);
                                  }
                                  if (type === 'locality') {
                                      $("#MYCITYFIELD").val(item.long_name);
                                  }
                                  if (type === 'administrative_area_level_1') {
                                      $("#MYSTATEFIELD").val(item.long_name);
                                  }
                              });
                          });
                      }
                  });
      
              });
      

    This work for me, if anyone have a better solutions are welcome :)