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

try{    Geocoder geo = new Geocoder(NewCommentActivity.this, Locale.getDefault());      List 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. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. Android(安卓)获取设备各种信息以及其它
  5. Android(安卓)Studio:正确引入so文件的方法
  6. android获取手机唯一识别号
  7. android基础(一) View与六大布局(上)
  8. http://www.eoeandroid.com/blog-546380-2248.html
  9. (OK) Android系统启动-SystemServer上篇

随机推荐

  1. Android AndroidX的迁移
  2. 如何从UA分辨出Android设备类型
  3. android 汉字转拼音 笔记
  4. phoneGap插件 .
  5. Android一次申请多个动态权限
  6. 深入浅出RxJava——在Android中使用响应
  7. Android 6.0耳机hook按键接听和挂断电话;
  8. Android下Bonjour服务的使用
  9. Android平台软件体系浅注
  10. Android入门之LinearLayout、AbsoluteLay