Google地图中国geocode地址解析

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

可以用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 即地图的左下和右上坐标。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值