Android 根据坐标获取地址

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值