0

i try to get the current zip code from the longitude and latitude which MyLocationOverlay delivers and set this to a EditView on my Activity.

The Activity crashs when i try to get the longitude and latitude from MyLocationOverlay.

Whats wrong with this code?

Regards, float

LogCat output: http://codepaste.net/vs6itk Line 59 is the following line:

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

Here is my Code:

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

    }
}

EDIT: I created a LocationListener to get my current location. Now the part crashes where i try to run gc.getFromLocation(latitude, longitude, 1); I can't read the Exception? :/

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

        }
    }

}
float
  • 1,265
  • 5
  • 22
  • 38
  • Post the log cat output. – Flo May 03 '11 at 12:24
  • here is the logcat output: http://codepaste.net/vs6itk Line 59 is the following line: double currentLatitude = myLocationOverlay.getMyLocation().getLatitudeE6(); – float May 03 '11 at 12:30

2 Answers2

0

If you are using an emulator API level 8 or 9, and getting the exception:

java.io.IOException: Service not Available

then it is a known bug, see service not available

It works OK on real devices and emulator level 7 though. ( You should probably put a trap on addresses being null too, though this won't make the geocoder work!)

NickT
  • 23,844
  • 11
  • 78
  • 121
  • Samsung Galaxy Tab uses Android 2.2 Froyo (API Level 8) and on this device my app crashs too. Unfortunatly i don't know how to get the log from this device... – float May 04 '11 at 07:01
0

Most likely the location being returned from

myLocationOverlay.getMyLocation()

or

Location location = locationManager.getLastKnownLocation(provider);

is NULL. Likely due to your application not yet having received a location fix, and having no previously saved locations.

Try moving the code block where you do the geocoding into your runnable that runs after receiving a fix. Like this:

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

            }
        }
    }
});
Clavicle
  • 152
  • 1
  • 10
  • myLocationOverlay.getMyLocation() returns a Geopoint. If i try to declare a Geocoder instance in the new runable(){} part im getting a compiler error "The constructor Geocoder(new Runnable(){}, Locale) is undefined" – float May 04 '11 at 06:34
  • Sorry about that. The Geocoder constructor wants a context, and when you put it in the runnable, "this" no longer refers to your activity. Try using getApplicationContext() instead. – Clavicle May 04 '11 at 12:33
  • thanks. Now it crashes when i try to get the Location. List
    addresses = gc.getFromLocation(currentLatitude, currentLongitute, 1); On the Emulator and the Samsung Galaxy Tab too.
    – float May 05 '11 at 13:30