根据地址查询经纬度

来源:互联网 发布:mac 百度云盘无法登陆 编辑:程序博客网 时间:2024/05/01 05:15

根据地址查询经纬度

    博客分类: 
  • android拾遗
android
有时候需要根据具体位置查询该地址对应的经纬度,然后将其保存到数据库中...... 
Java代码  收藏代码
  1. import android.location.Address;  
  2. import android.location.Geocoder;  
  3.   
  4. /** 
  5.      * 根据地址查询经纬度 
  6.      * @param strSearchAddress 查询地址 
  7.      * @return 
  8.      */  
  9.     private Address getGeoByAddress(String strSearchAddress) {  
  10.         Address address=null;  
  11.         try {  
  12.             if (strSearchAddress != null&&strSearchAddress.length()>0) {  
  13.                 Geocoder geocoder = new Geocoder(context,Locale.getDefault());  
  14. //              Geocoder geocoder = new Geocoder(context,Locale.US);  
  15.                 List<Address> addresses = geocoder.getFromLocationName(strSearchAddress, 1);  
  16.                 if (!addresses.isEmpty()) {  
  17.                     address = addresses.get(0);  
  18.                 }  
  19.             }  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         return address;  
  24.     }  


不是每次都能获取到经纬度的,有时候会抛出unable to parse response from server 异常。 
这是google map的bug,详情请见: 
http://code.google.com/p/android/issues/detail?id=8816 

当抛出异常后需要换一种方法解决, 
直接请求url获得json数据,然后解析: 
Java代码  收藏代码
  1. public JSONObject getLocationInfo(String address) {  
  2.   
  3.         HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+ address+ "&sensor=false");  
  4.         HttpClient client = new DefaultHttpClient();  
  5.         HttpResponse response;  
  6.         StringBuilder stringBuilder = new StringBuilder();  
  7.   
  8.         try {  
  9.             response = client.execute(httpGet);  
  10.             HttpEntity entity = response.getEntity();  
  11.             InputStream stream = entity.getContent();  
  12.             int b;  
  13.             while ((b = stream.read()) != -1) {  
  14.                 stringBuilder.append((char) b);  
  15.             }  
  16.         } catch (ClientProtocolException e) {  
  17.             e.printStackTrace();  
  18.             return null;  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.             return null;  
  22.         }  
  23.   
  24.         JSONObject jsonObject = new JSONObject();  
  25.         try {  
  26.             jsonObject = new JSONObject(stringBuilder.toString());  
  27.         } catch (JSONException e) {  
  28.             // TODO Auto-generated catch block  
  29.             e.printStackTrace();  
  30.             return null;  
  31.         }  
  32.   
  33.         return jsonObject;  
  34.     }  
  35.       
  36.     public Address getGeoPoint(JSONObject jsonObject) {  
  37.         if(jsonObject==null){  
  38.             return null;  
  39.         }  
  40.         Double lng = new Double(0);  
  41.         Double lat = new Double(0);  
  42.         try {  
  43.             lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)  
  44.                 .getJSONObject("geometry").getJSONObject("location")  
  45.                 .getDouble("lng");  
  46.   
  47.             lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)  
  48.                 .getJSONObject("geometry").getJSONObject("location")  
  49.                 .getDouble("lat");  
  50.         } catch (JSONException e) {  
  51.             // TODO Auto-generated catch block  
  52.             e.printStackTrace();  
  53.             return null;  
  54.         }  
  55.         Address address=new Address(Locale.US);  
  56.         address.setLatitude(lat);  
  57.         address.setLongitude(lng);  
  58.         return address;  
  59.     }  


在getGeoByAddress()方法的try catch中起一个线程执行getLocationInfo()方法: 
Java代码  收藏代码
  1. }catch (Exception e) {  
  2.     strSearchAddress=strSearchAddress.replace(" ""+").replace(","",+");  
  3.      DownloadTask task = new DownloadTask(strSearchAddress);//注意同步问题,要先返回,后操作。  
  4.      task.execute();  
  5. }  

最后使用的时候要注意线程同步! 
Java代码  收藏代码
  1. .........  
  2. Address latLong=getGeoByAddress(detailAddress);  
  3.             try {  
  4. //休眠2秒,好让请求url时有足够的时间返回经纬度。虽然是个很馊的主意,但至少有效。  
  5.                 Thread.sleep(2000);  
  6.             } catch (InterruptedException e) {  
  7.                 // TODO Auto-generated catch block  
  8.                 e.printStackTrace();  
  9.             }  
  10.   
  11.             if(latLong!=null){  
  12.                 if(latLong.getLatitude()!=0&&latLong.getLongitude()!=0){  
  13.                     App.store.setLatitude(latLong.getLatitude());//纬度  
  14.                     App.store.setLongitude(latLong.getLongitude());//经度  
  15.                 }  
  16.             }  
  17. .........  


Java代码  收藏代码
  1. class DownloadTask extends AsyncTask<String, Integer, JSONObject> {  
  2.         String strSearchAddress;  
  3.         DownloadTask(String strSearchAddress) {  
  4.             this.strSearchAddress=strSearchAddress;  
  5.         }  
  6.   
  7.         @Override  
  8.         protected JSONObject doInBackground(String... params) {  
  9.             JSONObject obj=getLocationInfo(strSearchAddress);  
  10.             return obj;  
  11.         }  
  12.   
  13.         @Override  
  14.         protected void onCancelled() {  
  15.             super.onCancelled();  
  16.         }  
  17.   
  18.         @Override  
  19.         protected void onPostExecute(JSONObject result) {  
  20.             Address address=getGeoPoint(result);  
  21.             if(address!=null){//赋值  
  22.                 if(address.getLatitude()!=0&&address.getLongitude()!=0){  
  23.                     App.store.setLatitude(address.getLatitude());//纬度  
  24.                     App.store.setLongitude(address.getLongitude());//经度  
  25.                 }  
  26.             }  
  27.         }  
  28.   
  29.         @Override  
  30.         protected void onPreExecute() {  
  31.             // 预处理  
  32.         }  
  33.   
  34.         @Override  
  35.         protected void onProgressUpdate(Integer... values) {  
  36.             // 更新进度  
  37.         }  
  38.     }  
0 0
原创粉丝点击