Android中使用Geocoding API

第一步:构造Geocoder API 需要的URL
URL格式
https://maps.googleapis.com/maps/api/geocode/output?parameters
其中output有两个选项json/xml,parameters部分有address/language/sensor等多个选项
URL示例
https://maps.googleapis.com/maps/api/geocode/json?address=北京&language=zh_CN&sensor=false
第二步:向API发送Http请求,返回一个json字符串
String uriAPI = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&language=%s&sensor=%s";
				//构造Geocoder API的完整URL
				String url = String.format(uriAPI, addr,"zh_CN","false");
				//创建request对象
				HttpGet request = new HttpGet(url);
				//创建HttpClient对象
				HttpClient client = new DefaultHttpClient();
				//得到请求响应对象
				HttpResponse response = client.execute(request);
				//若状态码为200,说明请求成功
				if(response.getStatusLine().getStatusCode()==200){
					//获得响应条目--该条目是json字符串
					String resultStr = EntityUtils.toString(response.getEntity());
					geoPoint=parseJson(resultStr);
				}


第三步:解析json字符串
//解析根元素,得到一个数组
			JSONArray jsonObjs = new JSONObject(str).getJSONArray("results");
			//取出数组中第一个json对象(本示例数组中实际只包含一个元素)
			JSONObject jsonObj = jsonObjs.getJSONObject(0);
			//解析得formatted_address值
			String address = jsonObj.getString("formatted_address");
			//解析得json对象中的geometry对象
			JSONObject geometry = jsonObj.getJSONObject("geometry");
			//解析得geometry对象中的location对象
			JSONObject location = geometry.getJSONObject("location");
			//解析得location对象中的latitude、longitude值
			String lat = location.getString("lat");
			String lng = location.getString("lng");
			dLati = Double.parseDouble(lat);
			dLong = Double.parseDouble(lng);

Google Geocoding API介绍

https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值