0

I am creating a service that will give updates on the users location every 5 minutes. I am using the DDMS to send the coordinates to the emulator. I want to convert the coordinates and find the location. How can i do this? I am new to android Please help. This is my code so far

 public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener ll = new MyLocListener();
    Location location = new Location("abc");
    ll.onLocationChanged(location ); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
    return START_STICKY;
}

 private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
    if (location != null) {
    Log.d("LOCATION CHANGED", location.getLatitude() + "");
    Log.d("LOCATION CHANGED", location.getLongitude() + "");
    }
    Toast.makeText(GetLocationService.this,
    location.getLatitude() + "" + location.getLongitude(),
    Toast.LENGTH_LONG).show();

    }
  }

}
Sindu_
  • 1,347
  • 8
  • 27
  • 67

1 Answers1

1

Try this:

try{
 Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
 List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
  if (addresses.isEmpty()) {
        yourtextfieldname.setText("Waiting for Location");
  }
  else {
     if (addresses.size() > 0) {       
        Log.d(TAG,addresses.get(0).getFeatureName() + ", 
         " + addresses.get(0).getLocality() +", 
         " + addresses.get(0).getAdminArea() + ",
         " + addresses.get(0).getCountryName());

     }
  }
}
catch (Exception e) {
    e.printStackTrace(); 
}
ductran
  • 10,043
  • 19
  • 82
  • 165
  • Thank you so much. There is a syntax mistake near .getFeatureName() – Sindu_ Jul 10 '12 at 07:22
  • I have checked, this mistake occur when I break line when post in this page. You should remove break line when copy and paste. – ductran Jul 10 '12 at 07:50