package com.hx.util.gaode; import com.hx.util.HttpUtil; import com.hx.util.StringUtils; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; /** * 高德地图工具 * @author mgchen * */ public class GaoDeMapUtil { /**获取经纬度*/ public static String URL_CODE = "https://restapi.amap.com/v3/geocode/geo"; /** * 获取地址的经纬度 * @param address * @param key * @return */ public static AddressCode addressCode(String address,String key) { AddressCode addressCode = null; //URL_CODE = URL_CODE+"?key="+key+"&address="+URLEncoder.encode(address,"UTF-8");//地址编译 URL_CODE = URL_CODE+"?key="+key+"&address="+address; JSONObject data = HttpUtil.HttpURLUtil(URL_CODE,null); if(data.optInt("status",0) == 1){ JSONArray geocodes = data.getJSONArray("geocodes"); if(geocodes.size()==1){ JSONObject locationObj = geocodes.getJSONObject(0); String location = locationObj.optString("location"); if(StringUtils.noNull(location)){ String[] locations = location.split(","); if(locations.length != 2){ return null; } addressCode = new AddressCode(); addressCode.setLng(locations[0]); addressCode.setLat(locations[1]); addressCode.setCityCode(locationObj.optString("citycode")); } } } return addressCode; } }