-
//输入中文地址,用高德api返回经纬度,返回值为字符串,(经度,纬度),无结果返回null public static String getLngAndLatByAmap(String address) throws Exception { address = address.trim(); String result = null; String url = "http://restapi.amap.com/v3/geocode/geo?address=" + URLEncoder.encode(address, "utf-8") + "&output=json&key=你所注册的key"; GetMethod method = new GetMethod(url); method.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 设置连接超时 int status = client.executeMethod(method); if (status == 200) { String json = method.getResponseBodyAsString(); System.out.println(json); JSONObject obj = JSONObject.parseObject(json); if (obj.get("status").toString().equals("1")) { if(obj.get("count").toString().equals("0")){ //无结果 result = null; }else{ //有结果 JSONArray array = obj.getJSONArray("geocodes"); String str = array.getString(0); JSONObject locationjson = JSONObject.parseObject(str); str = locationjson.getString("location"); String[] location = str.split(","); double lng = Double.parseDouble(location[0]); double lat = Double.parseDouble(location[1]); result = lng +","+lat; System.out.println("经度:" + lng + "---纬度:" + lat); } } else { System.out.println("未找到相匹配的经纬度!"); result = null; throw new Exception(); } } return result; }