Goole 地图 根据经纬度获取地址

package com.jueyue;

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

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;

public class HttpRequestUtil {

public static String getRequestByUrl(String strurl){
String strjson = "";
try {
URL url = new URL(strurl);
URLConnection conn = url.openConnection();
HttpURLConnection http = (HttpURLConnection)conn;
http.setRequestMethod("GET");
http.setDoInput(true);
http.setDoOutput(true);
http.connect();
InputStream in = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
String s = null;
while((s = br.readLine()) != null) {
strjson+=s;
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strjson;
}



private static HttpMethod getGetMethod(String latlng) throws IOException {
PostMethod post = new PostMethod("/maps/api/geocode/json");
GetMethod get = new GetMethod("/maps/api/geocode/json");

NameValuePair simcard = new NameValuePair("latlng", latlng);
NameValuePair simcard1 = new NameValuePair("sensor", "false");
NameValuePair simcard2 = new NameValuePair("language", "zh-CN");
get.setQueryString(new NameValuePair[] { simcard, simcard1,simcard2});

//InputStream input = new FileInputStream(new File("/home/ubuntu/my.txt"));
//"".getBytes("ISO8859-1")
//InputStream input = new StringBufferInputStream("my test aaaaaaaaaa");
//post.setRequestBody(input);
return get;
}
private static HttpMethod getPostMethod(String latlng) throws IOException {
PostMethod post = new PostMethod("/maps/api/geocode/json");

//latlng=40.714224,-73.961452&sensor=false&&language=zh-TW
NameValuePair simcard = new NameValuePair("latlng", latlng);
NameValuePair simcard1 = new NameValuePair("sensor", "false");
NameValuePair simcard2 = new NameValuePair("language", "zh-CN");

post.setRequestBody(new NameValuePair[] { simcard, simcard1,simcard2});
//InputStream input = new FileInputStream(new File("/home/ubuntu/my.txt"));
//"".getBytes("ISO8859-1")
//InputStream input = new StringBufferInputStream("my test aaaaaaaaaa");
//post.setRequestBody(input);
return post;
}

/**
* 根据经纬度获取地址
* @param latlng
* @return
*/
public static String getGoogleAddressBylatlng(String latlng){
String strAddress = "";
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("ditu.google.com", 80, "http");

HttpMethod method = null;
try {
method = getGetMethod(latlng);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// 使用GET方式提交数据
try {
client.executeMethod(method);
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "获取经纬度地址异常";
}
// 打印服务器返回的状态
int methodstatus = method.getStatusCode();
StringBuffer sb = new StringBuffer();
if(methodstatus == 200){
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),"UTF-8"));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);

}
org.json.JSONObject jo;
try {
jo = new org.json.JSONObject(sb.toString());
org.json.JSONArray ja = jo.getJSONArray("results");
org.json.JSONObject jo1 = ja.getJSONObject(0);
System.out.println(jo1.getString("formatted_address"));
strAddress = jo1.getString("formatted_address");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rd.close();
}catch (IOException e) {
throw new RuntimeException("error", e);
}
}
method.releaseConnection();
return strAddress;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = getRequestByUrl("http://ditu.google.com/maps/api/geocode/json?latlng=31.1899209667,121.3918055000&sensor=false&&language=zh-CN");
//String str = getRequestByUrl("http://ditu.google.com/maps/api/geocode/json?latlng=31.215912,121.477357&sensor=false&&language=zh-CN");
System.out.println(str);
/*String strhttp = HttpRequestUtil.getGoogleAddressBylatlng("31.1899209667,121.3918055000");
System.out.println(strhttp);*/
}

}

 

你可能感兴趣的:(Go)