小弟最近在做一些小android的开发,在开发地图的时候发现Geocoder根本就是个美丽的谎言,根本不能用。
无奈之下只好自己去下载google对应的geocode地址,比如http://maps.google.com/maps/api/geocode/xml?latlng=39.93,116.27&sensor=false 这个是给定的经纬度之后可以返回一个xml,这个xml文件包含了对应的地址信息。
浏览器打开之后,显示地址是“中国北京市海淀区永定河路”等一系列的内容。但是使用InputStreamReader isr = new InputStreamReader(url.openStream(), "GBK"); 打开相同的url的时候发现得到的xml里的地址信息全都是英文的,小弟不才弄了半天也没搞清楚怎么回事。
请各位大侠指教啊!

解决方案 »

  1.   

    可能是HTTP报头的问题
    以下是我截取该请求的TCP报文GET /maps/api/geocode/xml?latlng=39.93,116.27&sensor=false HTTP/1.1
    Host: 127.0.0.1
    User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: zh-cn,zh;q=0.5
    Accept-Encoding: gzip, deflate
    Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
    Keep-Alive: 115
    Connection: keep-alive
    Cache-Control: max-age=0你直接用流去读取的时候可能没有这些个信息,故而google服务器无法判定你的location所以给出了E文的信息
      

  2.   

    以下是我用JAVA代码实现该功能import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;public class Test
    {
        public static void main(String[] args) throws Exception
        {
            URL url = new URL("http://127.0.0.1/maps/api/geocode/xml?latlng=39.93,116.27&sensor=false");
            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            while(true)
            {
                String line = reader.readLine();
                if (line == null)
                {
                    break;
                }
                System.out.println(line);
            }
        }
    }
    其中截获的HTTP报文如下:GET /maps/api/geocode/xml?latlng=39.93,116.27&sensor=false HTTP/1.1
    User-Agent: Java/1.6.0_21
    Host: 127.0.0.1
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection: keep-alive返回的XML报文如下:<?xml version="1.0" encoding="UTF-8"?>
    <GeocodeResponse>
     <status>OK</status>
     <result>
      <type>route</type>
      <formatted_address>Yongdinghe Rd, Haidian, Beijing, China</formatted_address>
      <address_component>
       <long_name>永定河路</long_name>
       <short_name>永定河路</short_name>
       <type>route</type>
      </address_component>
      <address_component>
       <long_name>海淀区</long_name>
       <short_name>海淀区</short_name>
       <type>sublocality</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>Beijing</long_name>
       <short_name>Beijing</short_name>
       <type>locality</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>北京市</long_name>
       <short_name>北京市</short_name>
       <type>administrative_area_level_1</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>中国</long_name>
       <short_name>CN</short_name>
       <type>country</type>
       <type>political</type>
      </address_component>
      <geometry>
       <location>
        <lat>39.9301441</lat>
        <lng>116.2715891</lng>
       </location>
       <location_type>APPROXIMATE</location_type>
       <viewport>
        <southwest>
         <lat>39.9268240</lat>
         <lng>116.2680711</lng>
        </southwest>
        <northeast>
         <lat>39.9331193</lat>
         <lng>116.2743664</lng>
        </northeast>
       </viewport>
       <bounds>
        <southwest>
         <lat>39.9286631</lat>
         <lng>116.2698641</lng>
        </southwest>
        <northeast>
         <lat>39.9312802</lat>
         <lng>116.2725734</lng>
        </northeast>
       </bounds>
      </geometry>
     </result>
     <result>
      <type>sublocality</type>
      <type>political</type>
      <formatted_address>Haidian, Beijing, China</formatted_address>
      <address_component>
       <long_name>海淀区</long_name>
       <short_name>海淀区</short_name>
       <type>sublocality</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>Beijing</long_name>
       <short_name>Beijing</short_name>
       <type>locality</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>北京市</long_name>
       <short_name>北京市</short_name>
       <type>administrative_area_level_1</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>中国</long_name>
       <short_name>CN</short_name>
       <type>country</type>
       <type>political</type>
      </address_component>
      <geometry>
       <location>
        <lat>39.9597100</lat>
        <lng>116.2980120</lng>
       </location>
       <location_type>APPROXIMATE</location_type>
       <viewport>
        <southwest>
         <lat>39.8866433</lat>
         <lng>116.0488381</lng>
        </southwest>
        <northeast>
         <lat>40.1609855</lat>
         <lng>116.3950927</lng>
        </northeast>
       </viewport>
       <bounds>
        <southwest>
         <lat>39.8866433</lat>
         <lng>116.0488381</lng>
        </southwest>
        <northeast>
         <lat>40.1609855</lat>
         <lng>116.3950927</lng>
        </northeast>
       </bounds>
      </geometry>
     </result>
     <result>
      <type>administrative_area_level_1</type>
      <type>political</type>
      <formatted_address>Beijing, China</formatted_address>
      <address_component>
       <long_name>北京市</long_name>
       <short_name>北京市</short_name>
       <type>administrative_area_level_1</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>中国</long_name>
       <short_name>CN</short_name>
       <type>country</type>
       <type>political</type>
      </address_component>
      <geometry>
       <location>
        <lat>39.9042140</lat>
        <lng>116.4074130</lng>
       </location>
       <location_type>APPROXIMATE</location_type>
       <viewport>
        <southwest>
         <lat>39.4427581</lat>
         <lng>115.4234028</lng>
        </southwest>
        <northeast>
         <lat>41.0608158</lat>
         <lng>117.5146251</lng>
        </northeast>
       </viewport>
       <bounds>
        <southwest>
         <lat>39.4427581</lat>
         <lng>115.4234028</lng>
        </southwest>
        <northeast>
         <lat>41.0608158</lat>
         <lng>117.5146251</lng>
        </northeast>
       </bounds>
      </geometry>
     </result>
     <result>
      <type>locality</type>
      <type>political</type>
      <formatted_address>Beijing, China</formatted_address>
      <address_component>
       <long_name>Beijing</long_name>
       <short_name>Beijing</short_name>
       <type>locality</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>北京市</long_name>
       <short_name>北京市</short_name>
       <type>administrative_area_level_1</type>
       <type>political</type>
      </address_component>
      <address_component>
       <long_name>中国</long_name>
       <short_name>CN</short_name>
       <type>country</type>
       <type>political</type>
      </address_component>
      <geometry>
       <location>
        <lat>39.9042140</lat>
        <lng>116.4074130</lng>
       </location>
       <location_type>APPROXIMATE</location_type>
       <viewport>
        <southwest>
         <lat>39.6612714</lat>
         <lng>116.0119343</lng>
        </southwest>
        <northeast>
         <lat>40.2164962</lat>
         <lng>116.7829835</lng>
        </northeast>
       </viewport>
       <bounds>
        <southwest>
         <lat>39.6612714</lat>
         <lng>116.0119343</lng>
        </southwest>
        <northeast>
         <lat>40.2164962</lat>
         <lng>116.7829835</lng>
        </northeast>
       </bounds>
      </geometry>
     </result>
     <result>
      <type>country</type>
      <type>political</type>
      <formatted_address>China</formatted_address>
      <address_component>
       <long_name>中国</long_name>
       <short_name>CN</short_name>
       <type>country</type>
       <type>political</type>
      </address_component>
      <geometry>
       <location>
        <lat>35.8616600</lat>
        <lng>104.1953970</lng>
       </location>
       <location_type>APPROXIMATE</location_type>
       <viewport>
        <southwest>
         <lat>17.9996000</lat>
         <lng>73.4994137</lng>
        </southwest>
        <northeast>
         <lat>53.5609740</lat>
         <lng>134.7728100</lng>
        </northeast>
       </viewport>
       <bounds>
        <southwest>
         <lat>17.9996000</lat>
         <lng>73.4994137</lng>
        </southwest>
        <northeast>
         <lat>53.5609740</lat>
         <lng>134.7728100</lng>
        </northeast>
       </bounds>
      </geometry>
     </result>
    </GeocodeResponse>