简体   繁体   中英

Google places autocomplete returns translated results

I'm using google places autocomplete text, and untill yesterday it all went good. Since today, google places return a very few results in english even though it set to hebrew.

StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
            sb.append("?key=" + API_KEY);
            sb.append("&components=country:il");
            sb.append("&input=" + URLEncoder.encode(input, "utf8"));
            sb.append("&types=(cities)&language=il");
            URL url = new URL(sb.toString());
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }

While debugging, I can get the url which used to get the result, and when I'm checking the predictions (through the browser) it all looks good, and all the matches are in hebrew.

Once the jsonResults get the matches, in some case the first result is in english and the rest are in hebrew.

eg I write two letters in hebrew "יר" and the url matches return first place "ירושלים" (Jerusalem), but when it gets to the jsonResults the first result becomes "Jerusalem, Israel" which is different language from the original first result.

The issue was that google changed the Hebrew language definition from il to iw .

So it should be :

sb.append("&types=(cities)&language=iw");

List of supported languages: Google list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM