`

Google地图中国geocode地址解析

    博客分类:
  • java
阅读更多

地址解析:将地址转化为地理坐标过程,

可以用Google的API,但是这个有一定次数限制,具体可以查选google map官方说明。

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GoogleGeocoder {

    public static final String OUTPUT_JSON="json";
    public static final String OUTPUT_XML="xml";
    public static final String OUTPUT_KML="kml";
    public static final String OUTPUT_CSV="csv";
    static String url = "http://ditu.google.cn/maps/geo?sensor=false&oe=utf8&q=";
    public static String parseAddress(String admin,String query,String outputType) throws  IOException{
        String queryURL = url + admin+query+"&output="+outputType;
        URL myURL = null;
        URLConnection httpsConn = null;
        try {
            myURL = new URL(queryURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw e;
        }

        StringBuilder sb = new StringBuilder(4*4096);
      
            httpsConn = (URLConnection) myURL.openConnection();
            InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
            BufferedReader br = new BufferedReader(insr);
            String data = null;
            while ((data = br.readLine()) != null) {
                sb.append(data);
                if(sb.length()>=4*4096) break;
            }

httpsConn.close(); 
        return sb.toString();
    }

    public static void main(String argv[]) {
        
        try {
          String xml=   parseAddress("中国,北京市,","人民大学",OUTPUT_XML);
          System.out.println(xml);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 返回结果为XML(等价于KML),也可以改为JSON或者CSV。

上面是用请求输出按照UTF-8格式输出,然后用UTF-8格式读取输出流。

 

<?xml version="1.0" encoding="UTF-8" ?><kml xmlns="http://earth.google.com/kml/2.0"><Response>  <name>中国,北京市,人民大学</name>  <Status>    <code>200</code>    <request>geocode</request>  </Status>  <Placemark id="p1">    <address>中国北京市海淀区中关村大街59号中国人民大学</address>    <AddressDetails Accuracy="9" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中国</CountryName><AdministrativeArea><AdministrativeAreaName>北京市</AdministrativeAreaName><Locality><LocalityName>北京市</LocalityName><DependentLocality><DependentLocalityName>海淀区</DependentLocalityName><Thoroughfare><ThoroughfareName>中关村大街59号中国人民大学</ThoroughfareName></Thoroughfare><AddressLine>中国人民大学</AddressLine></DependentLocality></Locality></AdministrativeArea></Country></AddressDetails>    <ExtendedData>      <LatLonBox north="39.9795316" south="39.9624293" east="116.3301154" west="116.2981006" />    </ExtendedData>    <Point><coordinates>116.3141080,39.9709810,0</coordinates></Point>  </Placemark></Response></kml>

 

如果需要高效的解析方式,最好在查询参数中指定BOUNDER 即地图的左下和右上坐标。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics