【问题标题】:getting address from latitude and longitude从经纬度获取地址
【发布时间】:2013-07-04 16:49:24
【问题描述】:

我正在尝试从纬度和经度中获取用户的地址,如下所示:

LocationManager locationManager = (LocationManager)  
this.getSystemService(Context.LOCATION_SERVICE);
    // Define a listener that responds to location updates
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.

            lat =location.getLatitude();
            lon = location.getLongitude();


        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };




    try{
        Geocoder geocoder;

        geocoder = new Geocoder(this, Locale.getDefault());
        addresses=(geocoder.getFromLocation(lat, lon, 1));

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

    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getAddressLine(1);
    String country = addresses.get(0).getAddressLine(2);


    TextView tv = (TextView) findViewById(R.id.tv3);
    tv.setText(address+"\n"+city+"\n"+country);

// Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

每次运行时,我都会在“String address =addresses.get(0).getAddressLine(0);”的行上收到错误“IndexOutOfBoundsException: Invalid index 0, size is 0”。我了解该错误意味着我正在尝试访问不存在的内容,但我不确定是什么原因造成的。我一直在寻找获得地址的最佳方法,这就是我发现的,但它可能不是最有效或最好的方法。有什么建议?

【问题讨论】:

    标签: android location latitude-longitude


    【解决方案1】:

    纬度和经度仅在您的地理编码后触发的 onLocationchanged 中设置为各自的值,请尝试在之前设置纬度和经度,或者将地理编码放入 onLocationChanged 中。我看不到所有代码,但我猜您正在将 lat 和 lon 初始化为 0,并且地理编码器没有 0,0 的地址

    【讨论】:

    • 因此,如果用户发现自己在加纳海岸 (0,0) 外大西洋的一艘船上,代码无论如何都会崩溃 :)
    • :) 试试抓举吐司(你是谁?)
    • 将地理编码放入 onLocationChanged 工作!谢谢!
    【解决方案2】:

    根据 Android 文档

    处理地理编码和反向地理编码的类。地理编码是将街道地址或位置的其他描述转换为(纬度、经度)坐标的过程。反向地理编码是将(纬度、经度)坐标转换为(部分)地址的过程。反向地理编码位置描述中的详细信息量可能会有所不同,例如,一个可能包含最近建筑物的完整街道地址,而另一个可能仅包含城市名称和邮政编码。 Geocoder 类需要一个不包含在核心 android 框架中的后端服务。如果平台中没有后端服务,Geocoder 查询方法将返回一个空列表。使用 isPresent() 方法确定是否存在 Geocoder 实现。

    您没有在代码中测试Geocoder.isPresent()。很可能它只是没有在您的设备上实现,只是返回一个空列表。我从未使用过地理编码器,但我可以想象即使存在一个空列表也可能会返回它,比如您正在指定一个位于海洋中间的位置。在访问列表内容之前,您应该始终测试列表的大小:

    TextView tv = (TextView) findViewById(R.id.tv3);
    
    if (addresses.size() > 0) {
        String address = addresses.get(0).getAddressLine(0);
        String city = addresses.get(0).getAddressLine(1);
        String country = addresses.get(0).getAddressLine(2);
        tv.setText(address+"\n"+city+"\n"+country);
    } else {
        tv.setText("Oops...");
    }
    

    【讨论】:

    • 是的,这有助于防止应用程序崩溃,但不能解决问题。究竟什么是“未包含在核心 android 框架中的后端服务”?我在这里阅读了许多其他类似的问题,但都没有提到后端服务
    • @RSenApps,文档说的是空列表,而不是空列表
    • @Evgeny 这就是我的意思,列表是空的,这意味着必须有一个地理编码器,如果列表为空,那么我们知道地理编码器不存在,但他仍然应该实现 isPresent: )
    • @RSenApps 根据文档:The Geocoder query methods will return an **empty list** if there no backend service in the platform。与空值无关。
    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多