我正在构建一个应用程序,使用lat long通过Toast向用户显示当前地址。我正在使用Geocoder将lat long转换为地址,但是这种情况是,在某些设备上地址可以正常工作,但在某些设备上会崩溃。然后,我在崩溃的那些设备上调试该应用程序,发现在地址解析器行之后,它跳到catch(Exception),而没有执行其他行。
因此,还有其他方法可以长期获取位置信息。

还有另一个与here问题相同的问题,但是并没有给出太多解决方案,所以我再次询问。

我的Geocoder区块是

if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        try {
            geocoder = new Geocoder(this, Locale.getDefault());
            addresses = geocoder.getFromLocation(latitude,longitude,1);
            if (addresses != null && addresses.size() > 0){
                String address = addresses.get(0).getAddressLine(0);
                String city = addresses.get(0).getLocality();
                String state = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();
                String postalCode = addresses.get(0).getPostalCode();
                Toast.makeText(getApplicationContext(), "Your address is: " +address+ " "+city+ " " + state+ "\n" +country+ " "+ postalCode, Toast.LENGTH_LONG).show();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

最佳答案

您只需点击传递经度和纬度的google-maps网络服务即可。它只是一个GET-Method Web服务。

http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true

用您自己的lat&long代替lat和long。

10-05 21:14
查看更多