zoukankan      html  css  js  c++  java
  • Android中使用地图之地址(字符串类型)转换经纬度

    项目中用到根据距离排序,看百度地图帮助文档,试了几种方法都没有得以实现,最后通过网页版的json的以实现地址如下,传地址值即可

    http://maps.google.com/maps/api/geocode/json?address=%E5%8C%97%E4%BA%AC%E9%82%AE%E7%94%B5%E5%A4%A7%E5%AD%A6&sensor=false

     1 public JSONObject getLocationInfo(String address) {
     2         // 发送json请求获取地址的json对象
     3         HttpGet httpGet = new HttpGet("http://maps.google."
     4                 + "com/maps/api/geocode/json?address=" + address
     5                 + "ka&sensor=false");
     6         HttpClient client = new DefaultHttpClient();
     7         HttpResponse response;
     8         StringBuilder stringBuilder = new StringBuilder();
     9 
    10         try {
    11             response = client.execute(httpGet);
    12             HttpEntity entity = response.getEntity();
    13             InputStream stream = entity.getContent();
    14             int b;
    15             while ((b = stream.read()) != -1) {
    16                 stringBuilder.append((char) b);
    17             }
    18         } catch (ClientProtocolException e) {
    19         } catch (IOException e) {
    20         }
    21 
    22         JSONObject jsonObject = new JSONObject();
    23         try {
    24             jsonObject = new JSONObject(stringBuilder.toString());
    25         } catch (JSONException e) {
    26             // TODO Auto-generated catch block
    27             e.printStackTrace();
    28         }
    29         return jsonObject;
    30     }
    31 
    32     public GeoPoint getGeoPoint(JSONObject jsonObject) {
    33         // 解析json获取经纬度信息
    34         Double lon = new Double(0);
    35         Double lat = new Double(0);
    36 
    37         try {
    38             lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
    39                     .getJSONObject("geometry").getJSONObject("location")
    40                     .getDouble("lng");
    41 
    42             lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
    43                     .getJSONObject("geometry").getJSONObject("location")
    44                     .getDouble("lat");
    45 
    46         } catch (JSONException e) {
    47             e.printStackTrace();
    48         }
    49         return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
    50     }
  • 相关阅读:
    自制404页面
    Http错误代码含义
    数据访问基础类(基于Access数据库)
    NTILE函数在SQL Server 2000中的实现方法
    矩阵螺旋输出
    分区排名方案和排名值效率分析【图文+测试代码】
    安装eclipse axis2 插件(links 方式)
    perforce关闭服务后无法重启
    A4纸网页打印中对应像素的设定和换算
    2005数据库脚本在SQL2000上执行
  • 原文地址:https://www.cnblogs.com/snowspace/p/3293036.html
Copyright © 2011-2022 走看看