1

i am getting the latitude and longitude from currentlocation and display the city country and state name from latitude longitude using geocoder here i put the my logacat which have error

Log.error

05-15 12:13:40.584: E/Running(309): Mehod getBestProvider
05-15 12:13:40.674: E/Running(309): Mehod getBestProvider
05-15 12:13:57.083: E/Running(309): C Latitude27.422006
05-15 12:13:57.083: E/Running(309): C Longitude22.084095
05-15 12:13:57.093: E/Running(309): try called
05-15 12:13:57.093: E/Running(309): Exception called
05-15 12:13:57.093: W/System.err(309): java.io.IOException: Service not Available
05-15 12:13:57.113: W/System.err(309):at
     ndroid.location.Geocoder.getFromLocation(Geocoder.java:117)
05-15 12:13:57.113: W/System.err(309):  at 

Activityclass

 public class CitycountrymapActivity extends Activity implements LocationListener {
 private LocationManager locationManager;
 //private GeoPoint currentPoint;
 private Location currentLocation;
 double currentlatitude;
 double currentlongitude;
 String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getLocation();

}


 public void getLocation()
    {
        provider=getBestProvider();

        currentLocation=locationManager.getLastKnownLocation(provider);

        if(currentLocation!=null)
        {
            setCurrentLocation(currentLocation);

        }
        else
        {
            Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
        }

    }


    public String getBestProvider()
    {
        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        String bestprovider=locationManager.getBestProvider(criteria, true);
        Log.e("Running","Mehod getBestProvider");
        return bestprovider;
    }

    public void setCurrentLocation(Location location)
    {


        //currentlatitude=(double)(location.getLatitude());
        //currentlongitude=(double)(location.getLongitude());

        currentlatitude =27.422006;
        currentlongitude=22.084095;

        currentLocation = new Location("");

        //currentPoint = new GeoPoint((int)(72.454565* 1E6),(int)(23.4572448 * 1E6)); 
        //currentPoint  = new GeoPoint((int)(72.454565),(int)(23.4572448));

         Global.clat=currentlatitude;
         Global.clon=currentlongitude;

        Log.e("Running","C Latitude"+Global.clat);
        Log.e("Running","C Longitude"+Global.clon);

        try
        {


        Log.e("Running","try called");  

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        List<Address> addresses=geocoder.getFromLocation(currentlatitude, currentlongitude, 1);  

        Log.e("Running","geocoder set");
        Log.e("Running","addresses");

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

        Log.e("Running","Latitude"+currentlatitude);
        Log.e("Running","Longitude"+currentlongitude);
        Log.e("Running","Address"+address);
        Log.e("Running","City-->"+city);
        Log.e("Running","Country->"+country);

        }
        catch(Exception e)
        {
            Log.e("Running","Exception called");
            e.printStackTrace();
        }
    }

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        Toast.makeText(this, "Loction Changed", Toast.LENGTH_SHORT).show();
        setCurrentLocation(location);
    }

    protected void onResume()
    {
        super.onResume();
        locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
    }

    protected void onPause()
    {
        super.onPause();
        locationManager.removeUpdates(this);

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Provider Disabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Provider Enabled", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Staus Changed", Toast.LENGTH_SHORT).show();
    }

   }
Sunny
  • 97
  • 2
  • 9

2 Answers2

1

Please see this answer posted by@ddewaele. It will clear up the issue for you, and perhaps the workarounds will also help you get your application up and running.

Community
  • 1
  • 1
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • but currently i have no device how can i chek in emulator with android 2.2 apli level 8 – Sunny May 15 '12 at 09:19
  • hey one more question i get lat and long in this format Latitude23.132679999999997, Longitude72.20081833333333 but i want to in this format LAtitude = 23.132680 and Longitude 72.200818 how can i convert in this format – Sunny May 15 '12 at 11:12
  • I see that you print it out without any separator to the log in this format (at `Log.e("Running","Latitude"+currentlatitude);` and `Log.e("Running","Longitude"+currentlongitude); `), but what do you mean, you "get it in this format"? – rekaszeru May 15 '12 at 11:48
  • hey i got my answer double Latitude = 23.132679999999997; int precision = Math.pow(10, 6); double new_Latitude = double((int)(precision * Latitude))/precision; – Sunny May 15 '12 at 11:57
0
        locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0,
                    this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Log.v("Gps", "on");
        } else if (locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    100, 0, this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Log.v("network", "on");

        } else {
            Log.v("setting", "on");
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);

            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0,
                    this);
            curLocation = locMan
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

        if (curLocation != null) {
            clat = curLocation.getLatitude();
            clng = curLocation.getLongitude();
            Log.v("TAG  ", clat + "  " + clng);
        //  GeoPoint srcgeopoint = new GeoPoint((int) (clat * 1E6),
        //          (int) (clng * 1E6));


        } else {
            Toast.makeText(new_map.this, "No Location Find", Toast.LENGTH_SHORT);
        }


  //  srcLat = geoPoint.getLatitudeE6() / 1E6;
   //       srcLong = geoPoint.getLongitudeE6() / 1E6;

            Geocoder gCoder = new Geocoder(this);
            List<Address> addresses;
            try {
                addresses = gCoder.getFromLocation(clat, clng, 5);
                Log.e("get adderess", addresses + "");
                if (addresses != null && addresses.size() > 0) {
                    addres = addresses.get(0).getLocality();
                    System.out.print(addresses.get(0).getLocality());
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

add you code in city name set and get one ......

Android
  • 1,417
  • 9
  • 11