使用带有myLocationOverlay的地理编码器

时间:2011-05-03 12:12:30

标签: android geocoding mylocationoverlay

我尝试从MyLocationOverlay提供的经度和纬度获取当前的邮政编码,并将其设置为我的Activity上的EditView。

当我尝试从MyLocationOverlay获取经度和纬度时,活动崩溃。

这段代码有什么问题?

此致 浮

LogCat输出:http://codepaste.net/vs6itk 第59行是以下行:

 double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6(); 

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.partner_suche);

    final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
    final MapView mView = (MapView) findViewById(R.id.mapview);
    mView.getController().setZoom(14);

    List<Overlay> mapOverlays = mView.getOverlays();

    myLocationOverlay = new MyCustomLocationOverlay(this, mView);
    mapOverlays.add(myLocationOverlay);
    myLocationOverlay.enableMyLocation();
    myLocationOverlay.enableCompass();
    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mView.getController().animateTo(myLocationOverlay.getMyLocation());
        }
    });

    Geocoder gc = new Geocoder(this, Locale.getDefault());
    double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6();
    double currentLongitute = myLocationOverlay.getMyLocation().getLongitudeE6();

    try 
    {
        List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
        if (addresses.size() > 0) 
        {
            tView.setText(addresses.get(0).getLocality());
        }
    } catch (IOException e) 
    {

    }
}

编辑: 我创建了一个LocationListener来获取我当前的位置。现在,我尝试运行gc.getFromLocation(纬度,经度,1)时崩溃的部分;我看不懂异常? :/

LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    String provider = LocationManager.GPS_PROVIDER;

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
            } 

        public void onProviderDisabled(String provider){
            updateWithNewLocation(null);
            }
        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status,Bundle extras){ }
        };

    locationManager.requestLocationUpdates(provider, 0, 0, locationListener);

private void updateWithNewLocation(Location location) {
    final EditText tView = (EditText) findViewById(R.id.editTextPLZ);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    Geocoder gc = new Geocoder(this, Locale.getDefault());
    if (location != null) {
        try 
        {
            List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) 
            {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                    tView.setText(address.getPostalCode());
                }
            }
        } catch (Exception e) 
        {

        }
    }

}

2 个答案:

答案 0 :(得分:0)

如果您使用的是模拟器API级别8或9,并获得异常:

java.io.IOException:服务不可用

那么这是一个已知的错误,请参阅service not available

它可以在真实设备和模拟器7级上正常工作。 (您应该在地址上设置一个陷阱也为空,但这不会使地址解析器工作!)

答案 1 :(得分:0)

很可能是从

返回的位置
myLocationOverlay.getMyLocation()

Location location = locationManager.getLastKnownLocation(provider);

是NULL。可能由于您的应用程序尚未收到位置修复程序,并且之前没有保存位置。

尝试将执行地理编码的代码块移动到收到修复后运行的runnable中。像这样:

myLocationOverlay.runOnFirstFix(new Runnable() {
    public void run() {
        Location loc = myLocationOverlay.getMyLocation();
        if (loc != null) {
            mView.getController().animateTo(loc);
            Geocoder gc = new Geocoder(this, Locale.getDefault());
            double currentLatitude = loc.getLatitudeE6();
            double currentLongitute = loc.getLongitudeE6();

            try 
            {
                List<Address> addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1);
                if (addresses.size() > 0) 
                {
                    tView.setText(addresses.get(0).getLocality());
                }
            } catch (IOException e) 
            {

            }
        }
    }
});