Android中根据坐标获取地址,一般用Geocoder,大概像下面这样:

try{    Geocoder geo = new Geocoder(NewCommentActivity.this, Locale.getDefault());      List<Address> addresses = geo.getFromLocation(23.0897174,113.3116872, 1);      if (addresses.isEmpty()) {          Log.i("location", "addressed is Empty");      }      else {          if (addresses.size() > 0) {              Log.i("location", addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());          }      }  } 

当我使用这段代码在真机上运行时,报错:
java.io.IOException:Sevice not Available
at android.location.Geocoder.getFromLocation

Google了一下,都说是在模拟器上才有这个问题,真机不会有此问题。但我确确实实在真机上遇到了这个问题。
查看Android官方文档,发现人家已经说的很明白了:
The Geocoder API is available for this purpose. Note that behind the scene, the API is dependent on a web service. If such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper method calledisPresent() was added in Android 2.3 (API level 9) to check for the existence of the service.


看来对于没有这个服务的机型,确实是用不了这个方法的。那只能另辟蹊径了。在网上找到另外的方法:
访问Google的这个URL可以获得地址信息的JSON字符串。
String url = String.format("http://ditu.google.cn/maps/geo?output=json&key=abc&q=%s,%s", latitude, longitude);

更多相关文章

  1. Android 复习笔记之图解TextView类及其XML相关属性和方法
  2. android.support library找不到的解决方法
  3. Android项目中如果json字符串中key-value顺序很重要,不要使用Andr
  4. android 调用 react-native方法
  5. Eclipse build Android时不生成apk问题解决方法
  6. Android的线程使用来更新UI----View的几种更新方法(Thread、Hand
  7. android使用全局变量的两种方法

随机推荐

  1. android例子
  2. android时间控件DigitalClock的使用
  3. 关于WindowManager在Android(安卓)N和And
  4. Android对SlidingDraw组件修改
  5. Android常用URI以及URI简介
  6. Android Studio 基础控件使用
  7. Android SeekBar的使用
  8. 基于Android P 背光流程
  9. Android 获取天气预报
  10. Android onTouch事件