方式1: 根据经纬度获取: 省 市 区 位置名称
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/*
* 根据经纬度获取地址:省 市 区 位置名称
* */
public class AddressUtils {
public static void main(String[] args) {
Map<String, String> map = getAdd(39.988429, 116.4839);
System.out.println(JSON.toJSONString(map));
}
/**
* 根据经纬度获取位置信息
* @param latitude 纬度
* @param longitude 经度
* @return
*/
public static Map<String, String> getAdd(double latitude, double longitude) {
// type : 100代表道路,010代表POI(信息点),001代表门址,111可以同时显示前三项
String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + latitude + "," + longitude + "&type=010";
String add = "";
try {
URL url = new URL(urlString);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
java.io.BufferedReader in = new java.io.BufferedReader(
new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
add += line;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(add);
Map<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = JSONObject.fromObject(add);
JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
if(jsonArray.size() > 0){
JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0));
String allAdd = j_2.getString("admName");
String[] arr = allAdd.split(",");
if(arr.length > 0){
System.out.println("省:" + arr[0] + "
市:" + arr[1] + "
区:" + arr[2]);
map.put("county", arr[2]);//县/区
map.put("city", arr[1]);//市
map.put("province", arr[0]);//省
}
}
return map;
}
}
方式2: 根据经纬度获取: 位置名称 区 市 省 国家 邮编
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/*
* 根据经纬度获取地址:位置名称 区 市 省 国家 邮编
* */
public class AddCountryUtils {
public final static void main(String[] args) {
Map<String, String> map = GetLocationMsg(39.988429, 116.4839);
System.out.println(JSON.toJSONString(map));
}
/**
* 根据经纬度获取位置信息
* @param latitude 纬度
* @param longitude 经度
* @return
*/
public static Map<String, String> GetLocationMsg(double latitude, double longitude) {
String add = "";
String url = String.format("http://maps.google.cn/maps/api/geocode/json?latlng=%s,%s&language=CN", latitude,
longitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
while ((data = br.readLine()) != null) {
add = add + data;
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(add);
Map<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = JSONObject.fromObject(add);
if (jsonObject.getString("status").equals("OK")) {
JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("results"));
if (jsonArray.size() > 0) {
JSONObject job = JSONObject.fromObject(jsonArray.get(0));
JSONArray jar = JSONArray.fromObject(job.getString("address_components"));
if (jar.size() > 0) {
for (int i = 0; i < jar.size(); i++) {
JSONObject addjob = JSONObject.fromObject(jar.get(i));
String name = addjob.getString("long_name");
System.out.println(name);
int j = 0;
if(job.getString("formatted_address").contains("邮政编码")){
j = 1;
if(i == jar.size() - 1)
map.put("ZipCode", name);//邮编
}
if(i == jar.size() - 4 - j)
map.put("county", name);//县/区
if(i == jar.size() - 3 - j)
map.put("city", name);//市
if(i == jar.size() - 2 - j)
map.put("province", name);//省
if(i == jar.size() - 1 - j)
map.put("country", name);//国
}
}
}
}
return map;
}
}
转载自:http://blog.csdn.net/llllvvv/article/details/77235641