根据地址查询经纬度
2015-12-20 17:57
344 查看
根据地址查询经纬度
博客分类:android拾遗
android
有时候需要根据具体位置查询该地址对应的经纬度,然后将其保存到数据库中......
Java代码
![](http://gundumw100.iteye.com/images/icon_star.png)
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代码
![](http://gundumw100.iteye.com/images/icon_star.png)
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代码
![](http://gundumw100.iteye.com/images/icon_star.png)
}catch (Exception e) {
strSearchAddress=strSearchAddress.replace(" ", "+").replace(",", ",+");
DownloadTask task = new DownloadTask(strSearchAddress);//注意同步问题,要先返回,后操作。
task.execute();
}
最后使用的时候要注意线程同步!
Java代码
![](http://gundumw100.iteye.com/images/icon_star.png)
.........
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代码
![](http://gundumw100.iteye.com/images/icon_star.png)
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) {
// 更新进度
}
}
相关文章推荐
- MVC4 DropDownList (一) — 使用方法
- codevs 2833 奇怪的梦境
- linux下SVN安装与部署/读取/密码/用户详解
- hdoj--2138--How many prime numbers(暴力模拟)
- objectForKey与valueForKey在NSDictionary中的差异
- Factorial Trailing Zeroes
- hdoj--2138--How many prime numbers(暴力模拟)
- 锂电池电压和容量关系
- [Mysql数据库] mysql数据库使用trigger更新中间表
- 前端开发and设计学习网站
- 【杭电】[2026]首字母变大写
- 库函数atoi()的实现
- 20135317 信息安全系统设计基础期末总结
- 对于游戏服务器的一些思考和研究
- python核心编程-标准类派生-不可变类型
- JS组件系列——基于Bootstrap Ace模板的菜单和Tab页效果分享(你值得拥有)
- Objective-C语言——KVC设计模式
- 全面剖析Smarty缓存机制[三种缓存方式]
- 对OkHttp进行封装,实现了只查询缓存,网络请求失败自动查询本地缓存等功能
- win7下firefox浏览器不能使用