---
title: "在JAVA类中解析GOOGLE MAP地址和反向解析纬经度"
author: "topbox163"
date: "2010-05-05T15:57:37Z"
tags: [".net", "Google", "Java", "XML", "json"]
categories: ["编程语言"]
slug: "java-google-map"
draft: false
lastmod: "2010-05-05T15:57:37Z"
id: 659615
url: "https://topbox163.iteye.com/blog/659615"
---



import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GoogleTest {
	
	/**
	 * 解析地址和反解析纠经度
	 * @param addressOrPoint : 中英文地址，或者是 纬经度，不是经纬度
	 * @param outPutType :这三个数据类型 : xml、json、csv 注：输出格式建议用JSON或CSV，XML格式有时候不能正常显示，比如 中国：35.8616600,104.1953970
	 * @param googleKey : 如果项目发布在网络上，可能要从GOOGLE申请一个GOOGLE MAP KEY才能正常使用
	 * 参考：http://code.google.com/intl/zh-CN/apis/maps/documentation/services.html#Geocoding_Object
	 */
	private static void parseAddressAndPoint(String addressOrPoint, String outPutType, String googleKey){
		try {
			// http://ditu.google.cn 而不用 http;//map.google.com 原因是不能正常解析中文
			String url = "http://ditu.google.cn/maps/geo?output="+outPutType+"&q="+java.net.URLEncoder.encode(addressOrPoint,"UTF-8")+"&sensor=false&key="+googleKey;
			URL myUrl = new URL(url);
			URLConnection httpConn = myUrl.openConnection();
			InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(),"UTF-8");//读取数据，并设置数据编码
			int responseInt = isr.read();
			while(responseInt != -1){
				System.out.print((char)responseInt);
				responseInt = isr.read();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
//		GoogleTest.parseAddressAndPoint("福山路380号", "json", "abcdefg");
//		GoogleTest.parseAddressAndPoint("福山路380号", "xml", "abcdefg");
//		GoogleTest.parseAddressAndPoint("福山路380号", "csv", "abcdefg");
		GoogleTest.parseAddressAndPoint("31.2255527,121.5303352", "csv", "abcdefg"); //纬度,经度 而不是 经度,纬度
	}
}
