您的位置:首页 > 编程语言 > Java开发

(java功能篇)谷歌获取地址经纬度

2013-08-25 22:18 337 查看
package com.mohe.map;

import java.io.BufferedReader;

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

public String[] getCoordinate(String addr) {

String address = null;

try {

address = java.net.URLEncoder.encode(addr, "UTF-8");

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

}

String output = "csv";

String key = "ABQIAAAAevysxt9O5lBUCrSalm80MxQx8gmx0K-_Fjj4Tf8bNXH3BBSxZRRmI_CuZM2zQyuXEpG_uxt-aqPr-A";

String url = String.format(

"http://maps.google.com/maps/geo?q=%s&output=%s&key=%s",

address, output, key);

URL myURL = null;

URLConnection httpsConn = null;

// 进行转码

try {

myURL = new URL(url);

} catch (MalformedURLException e) {

}

try {

httpsConn = (URLConnection) myURL.openConnection();

if (httpsConn != null) {

InputStreamReader insr = new InputStreamReader(

httpsConn.getInputStream(), "UTF-8");

BufferedReader br = new BufferedReader(insr);

String data = null;

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

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

return retList;

}

insr.close();

}

} catch (IOException e) {

}

return null;

}

public static void main(String[] args) {

new Snippet().getCoordinate("查询地址");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: