工具类的书写:
public class BaiduMap {
public static Map getLngAndLat(String address){
address = address.replace(" ","");
Map map=new HashMap();
String url = "http://api.map.baidu.com/geocoder/v2/?address="+address+"&output=json&ak=spR5mgpswaHmDoZhyBwgu9ZN0QDQGd7h";
try {
String json = loadJSON(url);
JSONObject obj = JSONObject.parseObject(json);
if(obj.get("status").toString().equals("0")){
double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
map.put("lng", getDecimal(lng));
map.put("lat", getDecimal(lat));
//System.out.println("经度:"+lng+"---纬度:"+lat);
}else{
// LogUtil.debug("未找到相匹配的经纬度!");
map = null;
System.out.println("未找到相匹配的经纬度!");
}
}catch (Exception e){
// LogUtil.error("未找到相匹配的经纬度,请检查地址");
System.out.println("未找到相匹配的经纬度,请检查地址!");
map = null;
}
return map;
}
}
经纬度获取的代码:
String wholeAddress = (city + district + detail);
Map map= BaiduMap.getLngAndLat(wholeAddress);
String location = "";
BigDecimal lat = new BigDecimal(0);
BigDecimal lgn = new BigDecimal(0);
if (null != map && ""!=map.get("lng")&&""!=map.get("lat")){
System.out.println(map.get("lng"));//经度
System.out.println(map.get("lat"));//纬度
location = map.get("lng").toString()+","+ map.get("lat").toString();
lat = new BigDecimal(map.get("lat").toString());
lgn = new BigDecimal(map.get("lng").toString());
}
网友评论