Android 地址转换为经纬度

Posted on 2011/07/23

如果打算写一些LBS方面的的应用,可能用到地址与经纬度的互相转换。android.location.Geocoder类提供了此功能。实际上,Geocoder既提供了前向转换,也提供了后向转换——它可以获取地址并返回经纬度,也可以将经纬度对转换为一组地址。

可以通过各种方式来描述位置。例如,getFromLocationName()方法可以获得地方的名称、物理地址和机场编号,或者该位置的流行名称。这些方法提供了一个地址列表(List<Address>),而不是一个地址。因为这些方法返回多个结果,官方建议最好提供1~5的maxResults 值来限制结果集,下面我们来看一个查询地址的例子,和上次一样我们得自己定义一个类继承MapActivity。

我们可以试着写一个输入地名返回经纬度的程序。

在XML中定义一个输入框,一个按钮,一个MapView.

用户输入一个地名点击按钮,返回经纬度。

这是按钮的OnCliclk方法,其他省略。

publicvoidonClick( View v) {
try {
EditText loc =( EditText) findViewById( R . id . location);
String locationName = loc . getText (). toString();
List < Address > addressList = geocoder . getFromLocationName(

locationName,5);
if(addressList!=null&&addressList.size()>0){
intlat=(int) (addressList.get(0).getLatitude()*1E6);
intlng=(int) (addressList.get(0).getLongitude()*1E6);

GeoPointpt=newGeoPoint(lat,lng);
mapView.getController().setZoom(15);
mapView.getController().setCenter(pt);
}
}catch(Exceptione){
e.printStackTrace();
}
}

这样写感觉是没什么错,实际测试时发现,如果输入“tian an men”就可以返回经纬度,但输入“天安门”就会抛异常,说服务不可以用什么的,网上也有遇到这问题的人,eoe上边也有,但都不了了之了。国外也有输入英文抛异常的,http://code.google.com/p/android/issues/detail?id=8816,

在21楼发现了新的方法,完全放弃了getFromLocationName方法,用的是The Google Geocoding API

这个API整合可以代替先前的方法,输入地名返回JSON或者XML格式的文件,包含一对地址信息。

XML为http://maps.googleapis.com/maps/api/geocode/xml?address=北京交通大学&sensor=false

JSON为http://maps.googleapis.com/maps/api/geocode/json?address=北京交通大学&sensor=false

现在需要自己写一个静态方法去解析XML或者JSON,以JSON为例:

publicclassMapUtility {

publicstaticJSONObjectgetLocationInfo(Stringaddress){

HttpGethttpGet=newHttpGet("http://maps.google."

+"com/maps/api/geocode/json?address="+address

+"&sensor=false");

HttpClientclient=newDefaultHttpClient();

HttpResponseresponse;

StringBuilderstringBuilder=newStringBuilder();

try{

response=client.execute(httpGet);

HttpEntityentity=response.getEntity();

InputStreamstream=entity.getContent();

intb;

while((b=stream.read())!=-1){

stringBuilder.append((char) b);

}

}catch(ClientProtocolExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

JSONObjectjsonObject=newJSONObject();

try{

jsonObject=newJSONObject(stringBuilder.toString());

}catch(JSONExceptione){

e.printStackTrace();

}

returnjsonObject;

}

// converts JSONObject into a GeoPoint.

publicstaticGeoPointgetGeoPoint(JSONObjectjsonObject){

Doublelon=newDouble(0);

Doublelat=newDouble(0);

try{

lon=((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(JSONExceptione){

e.printStackTrace();

}

returnnewGeoPoint((int) (lat*1E6),(int) (lon*1E6));

}

}

第一个方法根据地址取得JSON,第二个方法从JSON提取坐标。

最后程序效果如下:

最后,应该考虑在不同于UI线程的线程中执行地理编码操作。很明显:这项操作很耗时,而且你一定不希望UI在进行地理编码时停顿,如果停顿会阻塞整个用户界面。当在执行一些耗时的操作的时候,不能及时地分发 事件,包括用户界面重绘事件。从用户的角度来看,应用程序看上去像挂掉了。更糟糕的是,如果阻塞应用程序的时间过长,Android会向用户提示 一些信息,即打开一个“应用程序没有相应(application not responding)” 的对话框。


更多相关文章

  1. Android(安卓)View 相关源码分析之三 View的绘制过程
  2. android Activity类的使用
  3. Android(安卓)App接入微信开放平台注意事项
  4. Android之Activities
  5. Android更新UI的两种方法(一)
  6. Android中程序与Service交互的方式——综述
  7. android:如何从照片中获取拍摄地址信息
  8. Android(安卓)Audio - 支持多个APK同时录音
  9. Android(安卓)从启动到程序运行发生的事情

随机推荐

  1. 用x种方式求第n项斐波那契数,99%的人只会
  2. Spark分区器HashPartitioner和RangeParti
  3. NAS迁移至OSS,目录迁移是顺序、随机判断
  4. 杂 | PMP项目管理认证
  5. 调用函数以及常用模块
  6. 好用的大数据平台有哪些?
  7. 如何用Python批量修改文件名?
  8. SumSwap独特的staking机制为何如此受欢迎
  9. 前端教程之Intro.js轻松实现新手引导效果
  10. Apache CarbonData 1.0.0发布及其新特性