How to get Google Geocoding API's response in language other then default (fx.:Chinese)? [closed]

送分小仙女□ 提交于 2020-01-03 05:50:21

问题


i have do it for several times to get address by parsing the Google json which contain infomation of addres in Chinese.But when i show the address in my mobile ,it is all in English.

i get the json from the url below. http://maps.googleapis.com/maps/api/geocode/json?address=wuhan&sensor=false

EDIT: With help of an answer i wana share with working solution:

            StringBuffer sb=new StringBuffer();
            sb.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=").append(latStr).append(',').append(lonStr).append("&sensor=false&Accept-Language:zh-CN");
            String url=sb.toString();
            HttpClient httpClient=new DefaultHttpClient();

            String responseData="";
            try {
                HttpResponse response=httpClient.execute(new HttpGet(url));
                response.addHeader("Accept-Language", "zh-CN");
                HttpEntity entity=response.getEntity();
                BufferedReader bf=new BufferedReader(new InputStreamReader((entity.getContent()),"UTF-8"));
                String line="";
                while((line=bf.readLine())!=null){
                    responseData=responseData+line;
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

回答1:


Just add Accept-Language to reaquest because in default google API will return information in english fx.: Accept-Language: zh-CN

EDIT (coz OP is wandering about):

if you're using java.net.HttpURLConnection connection; then use:

connection.setRequestProperty ( "Accept-Language", "zh-CN");

if org.apache.http.client.methods.HttpGet request; then:

request.addHeader("Accept-Language", "zh-CN");

i've just tested it in fiddler2

and for

GET /maps/api/geocode/json?address=wuhan&sensor=false HTTP/1.0
Host: maps.googleapis.com
Accept-Language: zh-CN

i get

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "武汉",
               "short_name" : "武汉",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "湖北省",
               "short_name" : "湖北省",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "中国",
               "short_name" : "CN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "中国湖北省武汉市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 31.36126030,
                  "lng" : 115.08257280
               },
               "southwest" : {
                  "lat" : 29.96907670,
                  "lng" : 113.70228110
               }
            },
            "location" : {
               "lat" : 30.5930870,
               "lng" : 114.3053570
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 30.78745989999999,
                  "lng" : 114.6189880
               },
               "southwest" : {
                  "lat" : 30.34877210,
                  "lng" : 113.9817810
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}


来源:https://stackoverflow.com/questions/8822089/how-to-get-google-geocoding-apis-response-in-language-other-then-default-fx-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!