Geocoding API 是一类简单的HTTP接口,用于提供从地址到经纬度坐标或者从经纬度坐标到地址的转换服务,用户可以使用C# 、C++、Java等开发语言发送HTTP请求且接收JSON、XML的返回数据。
Geocoding API包括地址解析和逆地址解析功能。
WEB 服务API:http://lbsyun.baidu.com/index.php?title=webapi
拾取坐标系统:http://api.map.baidu.com/lbsapi/getpoint/index.html
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lbl.SSHValidateHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* @Auther:
* @Date: 2018/9/12 19:08
* @Description:
*/
public class BaiDuUtil {
private static Logger logger = LoggerFactory.getLogger(BaiDuUtil.class);
private static final String BAIDU_APP_KEY = "*************";
/**
* 输入中文地址
*
* @param address
* @return lng(经度), lat(纬度)
*/
public static Map getLatitude(String address) {
String res;
try {
// 将地址转换成utf-8的16进制
address = URLEncoder.encode(address, "UTF-8");
URL resjson = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=" + address + "&ak=" + BAIDU_APP_KEY);
BufferedReader in = new BufferedReader(new InputStreamReader(resjson.openStream()));
StringBuilder sb = new StringBuilder();
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
if (SSHValidateHelper.isNotEmptyString(str)) {
Map map = new HashMap<>();
String subStr = str.substring(str.indexOf('(') + 1, str.indexOf("})") + 1);
//字符串转成json格式
JSONObject jsonObj = JSONObject.parseObject(subStr);
//获取经度
Object lng = jsonObj.getJSONObject("result").getJSONObject("location").get("lng");
//获取纬度
Object lat = jsonObj.getJSONObject("result").getJSONObject("location").get("lat");
map.put("lng", lng);
map.put("lat", lat);
//返回经纬度
return map;
}
} catch (Exception e) {
logger.error("地址解析出现异常!", e);
}
return null;
}
/**
* 地址的经纬度坐标 key lng(经度),lat(纬度)
*
* @param lng(经度),lat(纬度)
* @return 返回中文地址
*/
public static String getAddress(String lng, String lat) {
String res;
String address = null;
try {
URL resjson = new URL("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + lat + "," + lng + "&output=json&pois=1&ak=" + BAIDU_APP_KEY);
BufferedReader in = new BufferedReader(new InputStreamReader(resjson.openStream()));
StringBuilder sb = new StringBuilder();
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
in.close();
String str = sb.toString();
String subStr = str.substring(str.indexOf('(') + 1, str.indexOf("})") + 1);
JSONObject jsonObj = JSONObject.parseObject(subStr);
//获取匹配到的中文地址
JSONObject object = (JSONObject) jsonObj.getJSONObject("result").get("addressComponent");
if (!SSHValidateHelper.isEmpty(object)) {
String street_number = "";
if (!SSHValidateHelper.isEmpty(object.get("street_number"))) {
street_number = object.get("street_number").toString();
street_number=street_number.replace("号","")+"号";
}
// 省、市、区、街道号、拼接
address = object.get("province").toString() + object.get("city").toString() + object.get("district").toString() + object.get("street").toString() + street_number;
}
JSONArray array = (JSONArray) jsonObj.getJSONObject("result").get("pois");
if (!SSHValidateHelper.isEmpty(array)) {
JSONObject object1 = (JSONObject) array.get(0);
if (!SSHValidateHelper.isEmpty(object1)) {
// 详细地址
address = address + object1.get("name");
}
}
} catch (Exception e) {
logger.error("反向地址解析出现异常!", e);
}
return address;
}
public static void main(String args[]) {
Map map = BaiDuUtil.getLatitude("福建省福州市仓山区福建农林大学");
// //输出地址所对应的经纬度
// if (null != map) {
// System.out.print("福建省福州市闽侯县福州大学:");
// System.out.print("经度" + map.get("lng"));
// System.out.print(",纬度" + map.get("lat"));
// System.out.println();
// }
//输出经纬度对应的中文地址
// System.out.println("经度" + map.get("lng") + ",纬度" + map.get("lat") + " 对应的中文地址是:" + BaiDuUtil.getAddress("120.12385572053700000000","30.29511582271730000000"));
String[] split = "120.124036,30.295878".split(",");
System.out.println(BaiDuUtil.getAddress(split[0], split[1]));
}
}
返回Json:
renderReverse&&renderReverse({"status":0,"result":{"location":{"lng":120.16772499999994,"lat":30.259645057722925},"formatted_address":"浙江省杭州市西湖区湖滨路25号-1","business":"湖滨,平海路,南山路","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"浙江省","city":"杭州市","city_level":2,"district":"西湖区","town":"","adcode":"330106","street":"湖滨路","street_number":"25号-1","direction":"附近","distance":"32"},"pois":[{"addr":"浙江省杭州市西湖区西湖区龙井路","cp":"","direction":"内","distance":"0","name":"西湖风景区","poiType":"旅游景点","point":{"x":120.11827768820231,"y":30.22817665935107},"tag":"旅游景点;风景区","tel":"","uid":"df95f91c9c62cbb438221c0e","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"杭州市西湖区湖滨路附近三公园内","cp":" ","direction":"附近","distance":"12","name":"西湖音乐喷泉","poiType":"旅游景点","point":{"x":120.1676126268103,"y":30.25963337698464},"tag":"旅游景点;风景区","tel":"","uid":"1da21101df7930e57ab55c44","zip":"","parent_poi":{"name":"三公园","tag":"旅游景点;公园","addr":"杭州市西湖区湖滨路25号","point":{"x":120.167585677645,"y":30.259664571258424},"direction":"附近","distance":"15","uid":"9cedfced6486b87488f15d0f"}},{"addr":"杭州市西湖区湖滨路25号","cp":" ","direction":"附近","distance":"15","name":"三公园","poiType":"旅游景点","point":{"x":120.167585677645,"y":30.259664571258424},"tag":"旅游景点;公园","tel":"","uid":"9cedfced6486b87488f15d0f","zip":"","parent_poi":{"name":"杭州西湖风景名胜区-湖滨公园","tag":"旅游景点;公园","addr":"杭州市西湖区湖滨景区内","point":{"x":120.1635073706297,"y":30.266105974361588},"direction":"东南","distance":"952","uid":"c5e6aa998c3143c12fe67b84"}},{"addr":"杭州市上城区东坡路7号","cp":" ","direction":"西北","distance":"156","name":"杭州湖滨银泰in77A区","poiType":"购物","point":{"x":120.16898703424056,"y":30.25910307279917},"tag":"购物;购物中心","tel":"","uid":"1621f926e30e7dd20c0e6bdb","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"杭州市上城区湖滨路28号","cp":" ","direction":"西南","distance":"145","name":"杭州凯悦酒店","poiType":"酒店","point":{"x":120.16850194926518,"y":30.260553603853759},"tag":"酒店;快捷酒店","tel":"","uid":"e85e2527316e984a95f08c48","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"杭州市上城区湖滨路15号二公园内","cp":" ","direction":"西北","distance":"137","name":"湖滨晴雨","poiType":"旅游景点","point":{"x":120.16844805093457,"y":30.25877553053379},"tag":"旅游景点;景点","tel":"","uid":"5729963eb38c09687bf79671","zip":"","parent_poi":{"name":"二公园","tag":"旅游景点;公园","addr":"杭州市上城区湖滨路15号","point":{"x":120.16873550869777,"y":30.258354403142684},"direction":"西北","distance":"200","uid":"1bdbc724466943463686f468"}},{"addr":"杭州市上城区湖滨路15号","cp":" ","direction":"西北","distance":"200","name":"二公园","poiType":"旅游景点","point":{"x":120.16873550869777,"y":30.258354403142684},"tag":"旅游景点;公园","tel":"","uid":"1bdbc724466943463686f468","zip":"","parent_poi":{"name":"杭州西湖风景名胜区-湖滨公园","tag":"旅游景点;公园","addr":"杭州市西湖区湖滨景区内","point":{"x":120.1635073706297,"y":30.266105974361588},"direction":"东南","distance":"952","uid":"c5e6aa998c3143c12fe67b84"}},{"addr":"杭州市上城区学士路90","cp":" ","direction":"南","distance":"209","name":"乌江鱼川湘馆","poiType":"美食","point":{"x":120.1678461862429,"y":30.261278861265745},"tag":"美食;中餐厅","tel":"","uid":"34e4850886f5d4fbd9d891e4","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"学士路84号","cp":" ","direction":"南","distance":"221","name":"天堂伞(学士路)","poiType":"购物","point":{"x":120.16820550844689,"y":30.261317853446465},"tag":"购物;商铺","tel":"","uid":"23054fc8e5fe5696c3364471","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}},{"addr":"浙江省杭州市上城区蕲王路15号","cp":" ","direction":"南","distance":"274","name":"喜悦快捷酒店(西湖湖滨音乐喷泉店)","poiType":"酒店","point":{"x":120.16841211871417,"y":30.26169997598993},"tag":"酒店;快捷酒店","tel":"","uid":"e3a7fd13556e584b28b97c1f","zip":"","parent_poi":{"name":"","tag":"","addr":"","point":{"x":0.0,"y":0.0},"direction":"","distance":"","uid":""}}],"roads":[],"poiRegions":[{"direction_desc":"内","name":"西湖风景区","tag":"旅游景点;风景区","uid":"df95f91c9c62cbb438221c0e"}],"sematic_description":"西湖风景区内,西湖音乐喷泉附近12米","cityCode":179}})
判断对象、字符串、集合是否为空、不为空工具类SSHValidateHelper代码如下:
/**
* 判断对象、字符串、集合是否为空、不为空
*/
public final class SSHValidateHelper {
/**
* 判断数组是否为空
*
* @param array
* @return boolean
*/
@SuppressWarnings("unused")
private static boolean isEmptyArray(T[] array) {
if (array == null || array.length == 0) {
return true;
} else {
return false;
}
}
/**
* 判断数组是否不为空
*
* @param array
* @return boolean
*/
public static boolean isNotEmptyArray(T[] array) {
if (array != null && array.length > 0) {
return true;
} else {
return false;
}
}
/**
* 判断字符串是否为空
*
* @param string
* @return boolean
*/
public static boolean isEmptyString(String string) {
if (string == null || string.length() == 0) {
return true;
} else {
return false;
}
}
/**
* 判断字符串是否不为空
*
* @param string
* @return boolean
*/
public static boolean isNotEmptyString(String string) {
if (string != null && string.length() > 0) {
return true;
} else {
return false;
}
}
/**
* 判断集合是否为空
*
* @param collection
* @return boolean
*/
public static boolean isEmptyCollection(Collection> collection) {
if (collection == null || collection.isEmpty()) {
return true;
} else {
return false;
}
}
/**
* 判断集合是否不为空
*
* @param collection
* @return boolean
*/
public static boolean isNotEmptyCollection(Collection> collection) {
if (collection != null && !collection.isEmpty()) {
return true;
} else {
return false;
}
}
/**
* 判断map集合是否不为空
*
* @param map
* @return boolean
*/
@SuppressWarnings("rawtypes")
public static boolean isNotEmptyMap(Map map) {
if (map != null && !map.isEmpty()) {
return true;
} else {
return false;
}
}
/**
* 判断map集合是否为空
*
* @param map
* @return boolean
* @author ming.chen
*/
@SuppressWarnings("rawtypes")
public static boolean isEmptyMap(Map map) {
if (map == null || map.isEmpty()) {
return true;
} else {
return false;
}
}
/**
* 检验对象是否为空,String 中只有空格在对象中也算空.
*
* @param object
* @return 为空返回true, 否则false.
*/
@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object object) {
if (null == object) {
return true;
} else if (object instanceof String) {
return "".equals(object.toString().trim());
} else if (object instanceof Iterable) {
return !((Iterable) object).iterator().hasNext();
} else if (object.getClass().isArray()) {
return Array.getLength(object) == 0;
} else if (object instanceof Map) {
return ((Map) object).size() == 0;
} else if (Number.class.isAssignableFrom(object.getClass())) {
return false;
} else if (Date.class.isAssignableFrom(object.getClass())) {
return false;
} else {
return false;
}
}
}