第一步:构造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的完整URLString 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

更多相关文章

  1. EventBus源码解析
  2. Android(安卓)Handler消息处理顺序分析
  3. Android(安卓)使用AES/CBC/PKCS7Padding 加解密字符串
  4. android输入法02:openwnn源码解析02—Keyboard和KeyboardView
  5. perl 解析 android 字符串资源的 %1$s 匹配问题
  6. Android(安卓)Intent 基本使用及对象构成
  7. Android(安卓)中获取控件宽和高的方法(详细解析)
  8. 【Android】ArrayList通过remove方法删除元素对象源码分析
  9. 深入理解 Android(安卓)中的各种 Context

随机推荐

  1. [置顶] React Native Android(安卓)Cooki
  2. HTTP缓存
  3. Java中的泛型方法
  4. 搞懂Netty(3)编解码问题的解决
  5. 愿那只在天堂的猫,能够被温柔相待
  6. 凭啥Java的运行环境称虚拟机,Python的只能
  7. 数据结构之:二分搜索树
  8. ES Module
  9. 从webpack到rollup
  10. 视频断更的第45天,想你