博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java(百度地图API)使用坐标的经纬度得到具体的城市信息
阅读量:4045 次
发布时间:2019-05-25

本文共 2114 字,大约阅读时间需要 7 分钟。

百度API中的全球逆地理编码服务提供将坐标点(经纬度)转换为对应位置信息(所在行政区划,周边地标点分布)功能。

 访问的URL是:

http://api.map.baidu.com/reverse_geocoding/v3/?ak=您的ak&output=json&coordtype=wgs84ll

&location=纬度,经度  //GET请求

因为使用百度地图的API就要申请AK,申请链接如下 

 

//JSON转化为MAP函数中需要
net.sf.json-lib
json-lib
2.4
jdk15
//通过经纬度获得城市信息中需要
org.apache.httpcomponents
httpclient
4.5.11

 通过经纬度得到具体城市信息

public static Map
getcitydetailbyjingwei(double jing ,double wei) { Map
map = null; String url = "http://api.map.baidu.com/reverse_geocoding/v3/?" + "ak=你的AK&output=json&coordtype=wgs84ll&location=" +wei+","+jing; try { HttpClient client = HttpClientBuilder.create().build();//构建一个Client HttpGet get = new HttpGet(url.toString()); //构建一个GET请求 HttpResponse response = client.execute(get);//提交GET请求 HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体" String content = EntityUtils.toString(result); JSONObject res = JSONObject.fromObject(content); map = JsonUtil.parseJSON2Map(res); //通过下面的函数将json转化为map } catch (Exception e) { e.printStackTrace(); System.out.println("获取地址失败"); } return map; }

通过下面的方法将JSON转化为Map

import net.sf.json.JSONArray;import net.sf.json.JSONObject;import java.util.*;public class JsonUtil {    public static Map
parseJSON2Map(JSONObject json) { Map
map = new HashMap
(); // 最外层解析 for (Object k : json.keySet()) { Object v = json.get(k); // 如果内层还是数组的话,继续解析 if (v instanceof JSONArray) { List
> list = new ArrayList
>(); @SuppressWarnings("unchecked") Iterator
it = ((JSONArray) v).iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2)); } map.put(k.toString(), list); } else { map.put(k.toString(), v); } } return map; }}

输入经度和纬度:118.46566         36.55001

得到的结果

 

转载地址:http://xnzci.baihongyu.com/

你可能感兴趣的文章
常用排序算法总结(一) 比较算法总结
查看>>
剖析 Linux hypervisor
查看>>
SSH原理与运用
查看>>
程序员之深刻的思辨和严密的体系结构
查看>>
黄威地址的openeim001
查看>>
工人的工资少openeim002
查看>>
ye我们胜利了的shooow
查看>>
太白山可真雄伟的shooow
查看>>
只见他满身尘土的openeim
查看>>
故事从一只平凡的openeim002
查看>>
真是哑巴吃黄连的openeim001
查看>>
MainActivity 会异步加载图片到相应的ImageView上
查看>>
妈妈十分生气的shooow
查看>>
怎么写一个温泉管理系统
查看>>
令人神清气爽的shooow
查看>>
指导教师的shooow
查看>>
leetcode面试题01.06.字符串压缩,超出时间限制,样例通过31/32
查看>>
机器学习实战、第二章KNN算法详解、AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
查看>>
leetcode 535 TinyURL 的加密与解密 暴力 年轻人不讲武德—shooter7的博客
查看>>
课程设计(毕业设计)—基于机器学习KNN算法手写数字识别系统—计算机专业课程设计(毕业设计)
查看>>