通信人家园

 找回密码
 注册

只需一步,快速开始

短信验证,便捷登录

搜索

军衔等级:

  新兵

注册:2011-5-21
跳转到指定楼层
1#
发表于 2015-3-20 10:49:40 |只看该作者 |倒序浏览

第一步:构造Geocoder API 需要的URL
URL格式
https://maps.googleapis.com/maps/api/geocode/output?parameters
其中output有两个选项json/xml,parameters部分有address/language/******等多个选项
URL示例
https://maps.googleapis.com/maps/api/geocode/json?address=北京***&language=zh_CN&******=false
第二步:向API发送Http请求,返回一个json字符串
[java]
String uriAPI = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&language=%s&******=%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字符串
[java]
//解析根元素,得到一个数组
            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);

举报本楼

您需要登录后才可以回帖 登录 | 注册 |

版规|手机版|C114 ( 沪ICP备12002291号-1 )|联系我们 |网站地图  

GMT+8, 2025-8-6 23:17 , Processed in 0.113365 second(s), 17 queries , Gzip On.

Copyright © 1999-2025 C114 All Rights Reserved

Discuz Licensed

回顶部