如何用JAVA获取GOOGLE 地图经纬度,地址信息

 我来答
随梦而飞的鹤
2016-10-27 · 乐于分享互联网技术知识
随梦而飞的鹤
采纳数:492 获赞数:3377

向TA提问 私信TA
展开全部
第一步 、申请一个GOOGLE地图的KEY
1、根据地址获取经纬度

[java] view plain copy print?
public static void getGoogleLatLng() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 创建httpget.
HttpGet httpget = new HttpGet("https://maps.google.com/maps/api/geocode/json?address=上海市&sensor=false&key=");
logger.debug("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);

try {
// 获取响应实体
HttpEntity entity = response.getEntity();
logger.debug("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容
String str = EntityUtils.toString(entity);
JSONObject o = (JSONObject) JSON.parse(str);
JSONArray o2 = (JSONArray) o.get("results");
JSONObject o3 = (JSONObject) o2.get(0);
JSONObject o4 = (JSONObject) o3.get("geometry");
JSONObject o5 = (JSONObject)o4.get("location");

logger.debug("lat====>>>"+o5.get("lat")+";lng=====>>>"+o5.get("lng"));
}
logger.debug("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} catch (ParseException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
logger.debug(e.getMessage());
}
}
}

2、根据经纬度获取地址信息

[java] view plain copy print?
public static String getGoogleAddres(BigDecimal lat, BigDecimal lng) {
String addr = "";
if(null == lat || null == lng){
return addr;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
try {

// 创建httpget.
HttpGet httpget = new HttpGet(MessageFormat.format("https://maps.google.com/maps/api/geocode/json?latlng={0},{1}&sensor=false&&language=zh-CN&key=", lat, lng));
logger.debug("executing request " + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);

try {
// 获取响应实体
HttpEntity entity = response.getEntity();
logger.debug("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印响应内容
String str = EntityUtils.toString(entity);
JSONObject o = (JSONObject) JSON.parse(str);
JSONArray o1 = (JSONArray)o.get("results");

JSONObject o2 = (JSONObject)o1.get(0);
if(null != o2){
addr = String.valueOf(o2.get("formatted_address"));
logger.debug("详细地址====>>>"+addr);
JSONArray o3 = (JSONArray)o2.get("addressComponent");
logger.debug("地址明细====>>>"+JSONArray.toJSONString(o3));
}
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} catch (ParseException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.debug(e.getMessage());
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
logger.debug(e.getMessage());
}
}
return addr;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式