开发者

Geocoder not working by getfromlocationName

开发者 https://www.devze.com 2023-02-06 21:51 出处:网络
My code is as below: Geocoder geocoder = new Geocoder(this,Locale.UK ); List<Address> addresses = null;

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) {
 开发者_C百科   // 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.


Double check that

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

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


Further Reading

  • geocoder.getFromLocationName returns only null


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号