读书人

经过坐标获取地址信息

发布时间: 2012-09-24 13:49:41 作者: rapoo

通过坐标获取地址信息

测试url:

http://ditu.google.cn/maps/geo?output=csv&key=abcdef&hl=zh-CN&q=22.660648,113.171355

?

?

?

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 ditu {

?

??? public static void main(String[] args) {

??? ? String addr = geocodeAddr(29.840644,111.093750);//(38.9146943,121.612382);

??? ? System.out.println(addr);

??? }

?

??? public static String geocodeAddr(Double latitude, Double longitude) {

??? ? String addr = "";

??? ?

??? ? //密钥可以随便写一个key=abc

??? ? String url = String.format("http://ditu.google.cn/maps/geo?output=csv&key=abcdef&hl=zh-CN&q=%f,%f",latitude, longitude);

??? ? URL myURL = null;

??? ? URLConnection httpsConn = null;

??? ? try {

??? ?? myURL = new URL(url);

??? ? } catch (MalformedURLException e) {

??? ?? e.printStackTrace();

??? ?? return null;

??? ? }

??? ?

??? ? try {

??? ?? httpsConn = (URLConnection) myURL.openConnection();

??? ?? if (httpsConn != null) {

??? ??? InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());

??? ??? BufferedReader br = new BufferedReader(insr);

??? ??? String data = null;

??? ??? if ((data = br.readLine()) != null)

??? ??? {

??? ???? String[] retList = data.split(",");

??? ???? if (retList.length>2 && ("200".equals(retList[0]))) {

??? ????? addr = retList[2];

??? ???? }

??? ??? }

??? ??? insr.close();

??? ?? }

??? ? } catch (IOException e) {

??? ?? e.printStackTrace();

??? ?? return null;

??? ? }

??? ? return addr = addr.replace("\"", "");

??? ?}

?

}

读书人网 >移动开发

热点推荐