Java解析Google Maps API返回的Json
Google Maps API是一个很强大的东西,能够进行经纬度的查询和反查等,对于Java,Google提供了一个Webservices,地址为如下形式:
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
返回的是一个json(当然也可以选择XML),形式如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.42291810,
"lng" : -122.08542120
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42426708029149,
"lng" : -122.0840722197085
},
"southwest" : {
"lat" : 37.42156911970850,
"lng" : -122.0867701802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
|
Java解析Json需要借助一个第三方的包,org.json,把org.json的jar文件导入工程之后,添加如下语句就能使用了:
1
|
import org.json.*;
|
具体实现代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.*;
public class jsondemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String string = "";
String currentString;
while((currentString = bufferedReader.readLine())!=null)
{
string+=currentString;
}
JSONObject jsonObject = new JSONObject(string);
JSONArray resultsArray = jsonObject.getJSONArray("results");
for(int i = 0; i < resultsArray.length(); ++i)
{
JSONArray addressArray = resultsArray.getJSONObject(i).getJSONArray("address_components");
for(int l = 0; l < addressArray.length(); l++)
{
JSONObject addressObject = addressArray.getJSONObject(l);
String long_name = addressObject.getString("long_name");
System.out.print("long_name: ");
System.out.println(long_name);
JSONArray typesArray = addressObject.getJSONArray("types");
for(int j = 0; j < typesArray.length(); ++j)
{
System.out.print("types: ");
System.out.println(typesArray.getString(j));
}
String short_name = addressObject.getString("short_name");
System.out.print("short_name: ");
System.out.println(short_name);
}
String formattedString = resultsArray.getJSONObject(i).getString("formatted_address");
System.out.print("formatted_address: ");
System.out.println(formattedString);
JSONObject geometryObject = resultsArray.getJSONObject(i).getJSONObject("geometry");
JSONObject locationObject = geometryObject.getJSONObject("location");
System.out.print("lat: ");
System.out.println(locationObject.getDouble("lat"));
System.out.print("lng: ");
System.out.println(locationObject.getDouble("lng"));
System.out.print("location_type: ");
System.out.println(geometryObject.getString("location_type"));
JSONObject viewportObject = geometryObject.getJSONObject("viewport");
JSONObject northeastObject = viewportObject.getJSONObject("northeast");
System.out.print("northeast lat: ");
System.out.println(northeastObject.getDouble("lat"));
System.out.print("northeast lng: ");
System.out.println(northeastObject.getDouble("lng"));
JSONObject southwestObject = viewportObject.getJSONObject("southwest");
System.out.print("southwest lat: ");
System.out.println(southwestObject.getDouble("lat"));
System.out.print("southwest lng: ");
System.out.println(southwestObject.getDouble("lng"));
JSONArray typesArray = resultsArray.getJSONObject(i).getJSONArray("types");
for(int k = 0; k < typesArray.length(); k++)
{
System.out.print("types: ");
System.out.println(typesArray.getString(i));
}
}
System.out.print("status: ");
System.out.println(jsonObject.getString("status"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|
输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
long_name: 1600
types: street_number
short_name: 1600
long_name: Amphitheatre Parkway
types: route
short_name: Amphitheatre Pkwy
long_name: Mountain View
types: locality
types: political
short_name: Mountain View
long_name: Santa Clara
types: administrative_area_level_2
types: political
short_name: Santa Clara
long_name: California
types: administrative_area_level_1
types: political
short_name: CA
long_name: United States
types: country
types: political
short_name: US
long_name: 94043
types: postal_code
short_name: 94043
formatted_address: 1600 Amphitheatre Parkway, Mountain View, CA 94043, USA
lat: 37.4221913
lng: -122.0845853
location_type: ROOFTOP
northeast lat: 37.4235402802915
northeast lng: -122.0832363197085
southwest lat: 37.4208423197085
southwest lng: -122.0859342802915
types: street_address
status: OK
|
这只是实现了根据位置查询经纬度的功能,还有根据经纬度查位置的可以自行参考Google Maps API。
Java解析Google Maps API返回的Json相关推荐
- Java 调用Google Map Api解析地址,解析经纬度实例
Java 调用Google Map Api解析地址,解析经纬度实例 使用google地图的反向地址解析功能,提供一个经纬度得到对应地址,或者给出模糊地址,得到经纬度,放在java后台代码中处理,这个使 ...
- Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等
Google Maps API 网络服务 官网地址 : https://developers.google.com/maps/documentation/webservices/?hl=zh-cn 其 ...
- Google maps API开发
Google maps API开发 注:经纬度的查询,找了半天,终于找着活神仙了,(*^__^*) 嘻嘻-- 1.经纬度查询工具:http://www.playgoogle.com/googlemap ...
- Google Maps API –地图类型示例
Google Maps API支持四种地图类型: ROADMAP –显示普通的街道/道路地图(默认地图类型). 地形-根据地形信息显示正常的街道/道路地图. 卫星–仅显示卫星图像. 混合–普通和卫星视 ...
- Google Maps API V3 之 图层
图层概述 图层是地图上的对象,包含一个或多个单独项,但可作为一个整体进行操作.图层通常反映了您添加到地图上用于指定公共关联的对象集合.Maps API 会通过以下方法管理图层内对象的显示形式:将图层的 ...
- maps-api-v3_利用Google Maps API发挥创意
maps-api-v3 您已经设计了一个闪亮的新网站: 仔细选择颜色,版式和照片,以完美反映公司的品牌形象. 然后您的客户要求您添加地图. 当然,您可以使用地图构建"向导",例如每 ...
- 使用Google Maps API和google-maps-react进行React Apps
This tutorial aims at integrating the google maps API to your React components and enabling you to d ...
- Google Maps API v2 android版本开发 国内手机不支持google play Service相关问题解决--图文教程
Google Maps API v2 android版本开发 国内手机不支持google play Service相关问题解决--图文教程 参考文章: (1)Google Maps API v2 an ...
- Google Maps API编程资源大全
Google Maps API是Google自己推出编程API,可以让全世界对Google Maps有兴趣的程序设计师自行开发基于Google Maps的服务,建立自己的地图网站.以下是我在Googl ...
最新文章
- tuple 方法总结整理
- c++矩阵类_Python线性代数学习笔记——矩阵的基本运算和基本性质,实现矩阵的基本运算...
- 如何求对角矩阵的逆?
- boost::type_erasure::binding相关的测试程序
- Ray.tune可视化调整超参数Tensorflow 2.0
- Python的安装路径
- 返工在即,国家级“赛马”!多家技术公司发力,AI解决“大规模人群”零接触测温...
- 李迟2021年12月知识总结
- 我所理解的JVM(三):字节码的执行
- vuex state使用
- python典型安装_python安装某些第三方包报错解决办法
- 《大型网站技术架构:核心原理与案例分析》读书笔记
- 电路分析题目详解(四)
- 精益看板方法从理论到实战 (7)—— 控制在制品数量(下)
- 强化学习王者荣耀Ai的搭建
- 《点线SLAM系统》
- centos7使用蓝牙_centos7 下 通过终端 连接 蓝牙设备
- 浮点数与十六进制互相转换
- 《js遍历json、js创建table、隐藏id列、点击获取id值》
- 赶紧自查这个插件!黑客可能远程控制你的谷歌浏览器