0

i am new in android create current location and display its city name

i Have No idea About code so give some example to create current location and display also city name thnks in advance

  • Do some search on __Google__ – M D Mar 09 '15 at 10:23
  • yes but only latitude and longitude are display not display the current city i requred display current city @M D –  Mar 09 '15 at 10:25
  • You will get location Name using `Latitude` and `Longitude`. – M D Mar 09 '15 at 10:25
  • if you have some code than send me @ M D –  Mar 09 '15 at 10:26
  • [http://stackoverflow.com/questions/22323974/how-to-get-city-name-by-latitude-longitude-in-android](http://stackoverflow.com/questions/22323974/how-to-get-city-name-by-latitude-longitude-in-android) – M D Mar 09 '15 at 10:27
  • [http://stackoverflow.com/questions/14253737/get-city-name-from-latitude-and-longitude-in-android-gives-service-not-available](http://stackoverflow.com/questions/14253737/get-city-name-from-latitude-and-longitude-in-android-gives-service-not-available) – M D Mar 09 '15 at 10:27
  • A full project is here http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/ – Harry Mad Mar 09 '15 at 10:37
  • @harry Mad thnks but i can manually add latitude and longitude how can i get automatic latitude and longitude –  Mar 09 '15 at 11:12
  • Pls can clear me what is mean by automatic lat and long, because in current location you will get lag and long. – Harry Mad Mar 09 '15 at 11:20
  • Harry Mad How can i wat u p? –  Mar 09 '15 at 11:35
  • If it helped you. just upgrade my comments. thanks – Harry Mad Mar 09 '15 at 11:40
  • thanks Harry Mad its help me lots ... –  Mar 09 '15 at 11:46

1 Answers1

0
public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
{
   LocationClient mLocationClient;
private TextView addressLabel;
private TextView locationLabel;
private Button getLocationBtn;
private Button disconnectBtn;
private Button connectBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  locationLabel = (TextView) findViewById(R.id.locationLabel);
  addressLabel = (TextView) findViewById(R.id.addressLabel);
  getLocationBtn = (Button) findViewById(R.id.getLocation);

  getLocationBtn.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
        displayCurrentLocation();
     }
  });
  disconnectBtn = (Button) findViewById(R.id.disconnect);  
  disconnectBtn.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
        mLocationClient.disconnect();
        locationLabel.setText("Got disconnected....");
     }
  });
  connectBtn = (Button) findViewById(R.id.connect);  
  connectBtn.setOnClickListener(new View.OnClickListener() {
     public void onClick(View view) {
        mLocationClient.connect();
        locationLabel.setText("Got connected....");
     }
  });   
  // Create the LocationRequest object
   mLocationClient = new LocationClient(this, this, this);  
 }  
 @Override
 protected void onStart() {
  super.onStart();
  // Connect the client.
  mLocationClient.connect();
  locationLabel.setText("Got connected....");
}
@Override
protected void onStop() {
  // Disconnect the client.
  mLocationClient.disconnect();
  super.onStop();
  locationLabel.setText("Got disconnected....");
  }
  @Override
  public void onConnected(Bundle dataBundle) {
  // Display the connection status
  Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected() {
  // Display the connection status
  Toast.makeText(this, "Disconnected. Please re-connect.",
  Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
  // Display the error code on failure
  Toast.makeText(this, "Connection Failure : " + 
  connectionResult.getErrorCode(),
  Toast.LENGTH_SHORT).show();
}
public void displayCurrentLocation() {
  // Get the current location's latitude & longitude
  Location currentLocation = mLocationClient.getLastLocation();
  String msg = "Current Location: " +
  Double.toString(currentLocation.getLatitude()) + "," +
  Double.toString(currentLocation.getLongitude());

  // Display the current location in the UI
  locationLabel.setText(msg);

  // To display the current address in the UI
  (new GetAddressTask(this)).execute(currentLocation);
 }
/*
 * Following is a subclass of AsyncTask which has been used to get
 * address corresponding to the given latitude & longitude.
 */
 private class GetAddressTask extends AsyncTask<Location, Void, String>{
  Context mContext;
  public GetAddressTask(Context context) {
     super();
     mContext = context;
  }

  /*
   * When the task finishes, onPostExecute() displays the address. 
   */
  @Override
  protected void onPostExecute(String address) {
     // Display the current address in the UI
     addressLabel.setText(address);
  }
  @Override
  protected String doInBackground(Location... params) {
     Geocoder geocoder =
     new Geocoder(mContext, Locale.getDefault());
     // Get the current location from the input parameter list
     Location loc = params[0];
     // Create a list to contain the result address
     List<Address> addresses = null;
     try {
        addresses = geocoder.getFromLocation(loc.getLatitude(),
        loc.getLongitude(), 1);
     } catch (IOException e1) {
        Log.e("LocationSampleActivity", 
        "IO Exception in getFromLocation()");
        e1.printStackTrace();
        return ("IO Exception trying to get address");
     } catch (IllegalArgumentException e2) {
        // Error message to post in the log
        String errorString = "Illegal arguments " +
        Double.toString(loc.getLatitude()) +
        " , " +
        Double.toString(loc.getLongitude()) +
        " passed to address service";
        Log.e("LocationSampleActivity", errorString);
        e2.printStackTrace();
        return errorString;
     }
     // If the reverse geocode returned an address
     if (addresses != null && addresses.size() > 0) {
        // Get the first address
        Address address = addresses.get(0);
        /*
        * Format the first line of address (if available),
        * city, and country name.
        */
        String addressText = String.format(
        "%s, %s, %s",
        // If there's a street address, add it
        address.getMaxAddressLineIndex() > 0 ?
        address.getAddressLine(0) : "",
        // Locality is usually a city
        address.getLocality(),
        // The country of the address
        address.getCountryName());
        // Return the text
        return addressText;
     } else {
        return "No address found";
     }
  }
}// AsyncTask class
}

Refrence link http://www.tutorialspoint.com/android/android_location_based_services.htm