有时候需要根据具体位置查询该地址对应的经纬度,然后将其保存到数据库中......
import android.location.Address;
import android.location.Geocoder;
/**
* 根据地址查询经纬度
* @param strSearchAddress 查询地址
* @return
*/
private Address getGeoByAddress(String strSearchAddress) {
Address address=null;
try {
if (strSearchAddress != null&&strSearchAddress.length()>0) {
Geocoder geocoder = new Geocoder(context,Locale.getDefault());
// Geocoder geocoder = new Geocoder(context,Locale.US);
List<Address> addresses = geocoder.getFromLocationName(strSearchAddress, 1);
if (!addresses.isEmpty()) {
address = addresses.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return address;
}
不是每次都能获取到经纬度的,有时候会抛出unable to parse response from server 异常。
这是google map的bug,详情请见:
http://code.google.com/p/android/issues/detail?id=8816
当抛出异常后需要换一种方法解决,
直接请求url获得json数据,然后解析:
public JSONObject getLocationInfo(String address) { HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address="+ address+ "&sensor=false"); HttpClient client = new DefaultHttpClient(); HttpResponse response; StringBuilder stringBuilder = new StringBuilder(); try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } } catch (ClientProtocolException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } return jsonObject; } public Address getGeoPoint(JSONObject jsonObject) { if(jsonObject==null){ return null; } Double lng = new Double(0); Double lat = new Double(0); try { lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } Address address=new Address(Locale.US); address.setLatitude(lat); address.setLongitude(lng); return address; }
在getGeoByAddress()方法的try catch中起一个线程执行getLocationInfo()方法:
}catch (Exception e) { strSearchAddress=strSearchAddress.replace(" ", "+").replace(",", ",+"); DownloadTask task = new DownloadTask(strSearchAddress);//注意同步问题,要先返回,后操作。 task.execute(); }
最后使用的时候要注意线程同步!
......... Address latLong=getGeoByAddress(detailAddress); try { //休眠2秒,好让请求url时有足够的时间返回经纬度。虽然是个很馊的主意,但至少有效。 Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(latLong!=null){ if(latLong.getLatitude()!=0&&latLong.getLongitude()!=0){ App.store.setLatitude(latLong.getLatitude());//纬度 App.store.setLongitude(latLong.getLongitude());//经度 } } .........
class DownloadTask extends AsyncTask<String, Integer, JSONObject> { String strSearchAddress; DownloadTask(String strSearchAddress) { this.strSearchAddress=strSearchAddress; } @Override protected JSONObject doInBackground(String... params) { JSONObject obj=getLocationInfo(strSearchAddress); return obj; } @Override protected void onCancelled() { super.onCancelled(); } @Override protected void onPostExecute(JSONObject result) { Address address=getGeoPoint(result); if(address!=null){//赋值 if(address.getLatitude()!=0&&address.getLongitude()!=0){ App.store.setLatitude(address.getLatitude());//纬度 App.store.setLongitude(address.getLongitude());//经度 } } } @Override protected void onPreExecute() { // 预处理 } @Override protected void onProgressUpdate(Integer... values) { // 更新进度 } }
实现Android的不同精度的定位(基于网络和GPS)
http://lszdb1983.blog.163.com/blog/static/2042634820126221635756/
android Location 过滤最佳的位置
http://lipeng88213.iteye.com/blog/1936338
标签:null,return,经纬度,strSearchAddress,地址,address,catch,new,查询 From: https://blog.51cto.com/u_5454003/6174179