google map根据地址获取经纬度

简介:

最近在做google map 根据地址获取经纬度的时候使用到Geocoder类

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. /**       
  4.      * 将地址转为经纬度       
  5.      * @param address       
  6.      * @return       
  7.      */        
  8.     public GeoPoint getGeoPointByAddress(String address) {        
  9.         GeoPoint geoPoint = null;        
  10.         if (address != "") {        
  11.             Geocoder myGeocoder = new Geocoder(RegisterAccountActivity.this);        
  12.             List<Address> addressList = null;        
  13.             try {        
  14.                 addressList = myGeocoder.getFromLocationName(address, 1);        
  15.                 if (!addressList.isEmpty()) {        
  16.                     Address tempAddress = addressList.get(0);        
  17.                     double myLatitude = tempAddress.getLatitude() * 1E6;        
  18.                     double myLongitude = tempAddress.getLongitude() * 1E6;        
  19.                     geoPoint = new GeoPoint((int) myLatitude, (int) myLongitude);        
  20.                 }        
  21.             } catch (Exception e) {        
  22.                 e.printStackTrace();        
  23.             }        
  24.         }        
  25.         return geoPoint;        
  26.     }    

这段代码对2.2和2.3系统居然不起作用,报java.io.IOException: Service not Available的错误,换用2.1系统却可以。

估计这是个bug。于是在网上找到另一种解决方法:

 

 
  1. Java 代码复制内容到剪贴板  
  2.       
  3. public static JSONObject getLocationInfo(String address) {        
  4.         
  5.         HttpGet httpGet = new HttpGet(        
  6.                 "http://maps.g...ddress="        
  7.                         + address + "ka&sensor=false");        
  8.         HttpClient client = new DefaultHttpClient();        
  9.         HttpResponse response;        
  10.         StringBuilder stringBuilder = new StringBuilder();        
  11.         
  12.         try {        
  13.             response = client.execute(httpGet);        
  14.             HttpEntity entity = response.getEntity();        
  15.             InputStream stream = entity.getContent();        
  16.             int b;        
  17.             while ((b = stream.read()) != -1) {        
  18.                 stringBuilder.append((char) b);        
  19.             }        
  20.         } catch (ClientProtocolException e) {        
  21.         } catch (IOException e) {        
  22.         }        
  23.         
  24.         JSONObject jsonObject = new JSONObject();        
  25.         try {        
  26.             jsonObject = new JSONObject(stringBuilder.toString());        
  27.         } catch (JSONException e) {        
  28.             e.printStackTrace();        
  29.         }        
  30.         return jsonObject;        
  31.     }        
  32.         
  33.     public static GeoPoint getGeoPoint(JSONObject jsonObject) {        
  34.         Double lon = new Double(0);        
  35.         Double lat = new Double(0);        
  36.         try {        
  37.             lon = ((JSONArray) jsonObject.get("results")).getJSONObject(0)        
  38.                     .getJSONObject("geometry").getJSONObject("location")        
  39.                     .getDouble("lng");        
  40.         
  41.             lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)        
  42.                     .getJSONObject("geometry").getJSONObject("location")        
  43.                     .getDouble("lat");        
  44.         
  45.         } catch (JSONException e) {        
  46.             e.printStackTrace();        
  47.         }        
  48.         return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));        
  49.     }    

这样就解决了问题。







     本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962513,如需转载请自行联系原作者



相关文章
|
3月前
Google Earth Engine(GEE)——从列表中少选所需要的数字不用map函数,还能如何实现?简单方法介绍
Google Earth Engine(GEE)——从列表中少选所需要的数字不用map函数,还能如何实现?简单方法介绍
18 0
|
2天前
|
Android开发
如何在Android真机上检测是否有Google Map add-on
如何在Android真机上检测是否有Google Map add-on
11 3
|
2天前
|
Shell
android2.3.4没有google map的真机上增加google map(原创)
android2.3.4没有google map的真机上增加google map(原创)
13 4
|
3月前
Google Earth Engine(GEE)——如何建立一个逐日的时序图表chart用map进行遍历
Google Earth Engine(GEE)——如何建立一个逐日的时序图表chart用map进行遍历
20 0
|
7月前
|
数据处理 开发者 Python
Google Earth Engine Map教程书中英双语
Google Earth Engine Map教程书中英双语
141 0
|
前端开发
Google Earth Engine(GEE)——如何加载ee.FeatureCollection中点数据在MAP中
Google Earth Engine(GEE)——如何加载ee.FeatureCollection中点数据在MAP中
514 0
Google Earth Engine(GEE)——如何加载ee.FeatureCollection中点数据在MAP中
|
搜索推荐
Google设置新标签页默认地址
Google设置新标签页默认地址
Google设置新标签页默认地址
|
Ubuntu JavaScript 前端开发
Google Map api国内正常使用该如何配置(2021最新)
Google Map api国内正常使用该如何配置(2021最新)
367 0
Google Map api国内正常使用该如何配置(2021最新)
|
大数据 atlas
Google Earth Engine——Oxford MAP TCW: Malaria Atlas Project Gap-Filled Tasseled Cap Wetness数据集
Google Earth Engine——Oxford MAP TCW: Malaria Atlas Project Gap-Filled Tasseled Cap Wetness数据集
126 0
Google Earth Engine——Oxford MAP TCW: Malaria Atlas Project Gap-Filled Tasseled Cap Wetness数据集
|
机器学习/深度学习 数据挖掘 定位技术
Google Earth Engine ——数据全解析专辑(Global Map of Oil Palm Plantations)全球油棕种植园数据集!
Google Earth Engine ——数据全解析专辑(Global Map of Oil Palm Plantations)全球油棕种植园数据集!
119 0
Google Earth Engine ——数据全解析专辑(Global Map of Oil Palm Plantations)全球油棕种植园数据集!