package com.vtradex.tms.server.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.json.JSONObject;
/**
*/
public class BaiduGeocoder {
public static String URL_ADDRESS = "http://api.map.baidu.com/geocoder?address={0}&output=json&key=76fc7754b6eeea437b33eed76150946c";
public static String URL_LOCATION = "http://api.map.baidu.com/geocoder?output=json&location={0}&key=37492c0ee6f924cb5e934fa08c6b1676";
public static void main(String[] args) throws Exception {
//经纬度获取地址
double lat = 31.218782;
double lng = 121.403227;
JSONObject result = parseLocation(lat, lng);
JSONObject location = result.getJSONObject("location");
System.out.println("纬度:" + location.getDouble("lat"));
System.out.println("经度:" + location.getDouble("lng"));
String formatted_address = result.getString("formatted_address");
System.out.println("地址:" + formatted_address);
JSONObject addressComponent = result.getJSONObject("addressComponent");
System.out.println(addressComponent);
//地址解析经纬度
// String address = "上海市天山路";
// JSONObject result = parseAddress(address);
// JSONObject location = result.getJSONObject("location");
// System.out.println("纬度:" + location.getDouble("lat"));
// System.out.println("经度:" + location.getDouble("lng"));
}
public static String getURL(boolean f,String value ){
String token = "{0}" ;
if(f){
return URL_ADDRESS.replace(token, value);
}
return URL_LOCATION.replace(token, value);
}
//经纬度反解析地址
public static JSONObject parseLocation(Double lat, Double lng) throws UnsupportedEncodingException, JSONException{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(false,lat + "," + lng));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 地址解析经,纬度
public static JSONObject parseAddress(String address) throws UnsupportedEncodingException, JSONException{
address = URLEncoder.encode(address, "UTF-8");
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(true,address));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.json.JSONObject;
/**
*/
public class BaiduGeocoder {
public static String URL_ADDRESS = "http://api.map.baidu.com/geocoder?address={0}&output=json&key=76fc7754b6eeea437b33eed76150946c";
public static String URL_LOCATION = "http://api.map.baidu.com/geocoder?output=json&location={0}&key=37492c0ee6f924cb5e934fa08c6b1676";
public static void main(String[] args) throws Exception {
//经纬度获取地址
double lat = 31.218782;
double lng = 121.403227;
JSONObject result = parseLocation(lat, lng);
JSONObject location = result.getJSONObject("location");
System.out.println("纬度:" + location.getDouble("lat"));
System.out.println("经度:" + location.getDouble("lng"));
String formatted_address = result.getString("formatted_address");
System.out.println("地址:" + formatted_address);
JSONObject addressComponent = result.getJSONObject("addressComponent");
System.out.println(addressComponent);
//地址解析经纬度
// String address = "上海市天山路";
// JSONObject result = parseAddress(address);
// JSONObject location = result.getJSONObject("location");
// System.out.println("纬度:" + location.getDouble("lat"));
// System.out.println("经度:" + location.getDouble("lng"));
}
public static String getURL(boolean f,String value ){
String token = "{0}" ;
if(f){
return URL_ADDRESS.replace(token, value);
}
return URL_LOCATION.replace(token, value);
}
//经纬度反解析地址
public static JSONObject parseLocation(Double lat, Double lng) throws UnsupportedEncodingException, JSONException{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(false,lat + "," + lng));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// 地址解析经,纬度
public static JSONObject parseAddress(String address) throws UnsupportedEncodingException, JSONException{
address = URLEncoder.encode(address, "UTF-8");
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(getURL(true,address));
try {
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
String str = postMethod.getResponseBodyAsString();
JSONObject json = new JSONObject(str);
return json.getJSONObject("result");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}