您的位置:首页 > 其它

根据地址查询经纬度

2015-12-20 17:57 344 查看


根据地址查询经纬度

博客分类:

android拾遗

android

有时候需要根据具体位置查询该地址对应的经纬度,然后将其保存到数据库中......

Java代码


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数据,然后解析:

Java代码


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()方法:

Java代码


}catch (Exception e) {

strSearchAddress=strSearchAddress.replace(" ", "+").replace(",", ",+");

DownloadTask task = new DownloadTask(strSearchAddress);//注意同步问题,要先返回,后操作。

task.execute();



最后使用的时候要注意线程同步!

Java代码


.........

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());//经度

}

}

.........

Java代码


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) {

// 更新进度

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: