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     }

 

posted @ 2013-08-31 12:27  雪地深处  阅读(1031)  评论(0编辑  收藏  举报