0

My code is as below:

Geocoder geocoder = new Geocoder(this,Locale.UK );
         List<Address> addresses = null;
         Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 
   try {
    Toast.makeText(displaybyName.this, "country: before " , Toast.LENGTH_SHORT).show(); 

    addresses = geocoder.getFromLocationName( 
      "Camden", 1);

    if (addresses.size() > 0) {

           geopoint = new GeoPoint(
                         (int) (addresses.get(0).getLatitude() * 1E6), 
                         (int) (addresses.get(0).getLongitude() * 1E6));

           //Animating on display

           mapcontroller.animateTo(geopoint);
           mapcontroller.setZoom(5); 
           mapview.invalidate();
          } 


   } catch (IOException io) {
    // TODO Auto-generated catch block
    Toast.makeText(displaybyName.this, "Connection Error", Toast.LENGTH_SHORT).show();
   }

   if (addresses.size() > 0) {

          geopoint = new GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

          //Animating on display

          mapcontroller.animateTo(geopoint);
          mapcontroller.setZoom(5); 
          mapview.invalidate();
         } 

However, the app is crashing each time. The geocoder is not returning any lat/long.

Is there any issue with the codes...plz plz advise.

dogbane
  • 266,786
  • 75
  • 396
  • 414
Kam
  • 1
  • 1

2 Answers2

1

Double check that

addresses = geocoder.getFromLocationName("Camden", 1);

returns something that isn't null - otherwise: the following line will raise an NullPointerException


Further Reading

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Thanks, i tried several different address but still my app is crashing. I have checked the permissions on Manifest files and it seems to be okie.. I have some addresses from london city and needs to get its coordinates to be able to display it on the map. Is there any other way i can do this because the geocoder function is not working. – Kam Jan 24 '11 at 22:37
  • When trying to get a coordinates out of an address,the program is crashing on the emulator.I cannot see an error. The Geocoder class requires a backend service that is not included in the core android framework. Sooo, i am confuzed now. What do I have to call, import, add, use in code.... to make this work? I am using Google Api2.2, Api level 8. If somebody has found a solution for this, or a pointer for documentation, something that i didn't discover, please let us know. Thank you for your time. – Kam Jan 24 '11 at 23:46
0

There is android version problem because geocoder doesn't come with the core android framework but this will work fine on old and new versions like on 1.6, 2.0 and API level 15 try these first it will definitely work but still insisting on just try your own geocoder class using JSON.