Hello Everyone,
Can any one help me for getting current Location
(latitude and longitude)
with Address i try the below code but it always return me same old Address
like
right now me in city A but it always return me old City B address here is
the code which i use:
package com.lbsdemo;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class LBSDemo extends Activity {
LocationManager locationManager;
Geocoder geocoder;
StringBuilder strReturnedAddress = new StringBuilder();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,
locationListener);
Location location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if(addresses != null)
{
for (int j=0; j<1; j++)
{
Address returnedAddress = addresses.get(j);
for(int i=0;
i<returnedAddress.getMaxAddressLineIndex(); i++)
{
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append(",");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Current Address", latitude + ":" + longitude + ":" +
strReturnedAddress);
}
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.txt);
String latLongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng,
1);
if(addresses != null)
{
for (int j=0; j<1; j++)
{
Address returnedAddress = addresses.get(j);
for(int i=0;
i<returnedAddress.getMaxAddressLineIndex(); i++)
{
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append(",");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
latLongString = "Lat:" + lat + "\nLong:" + lng + "\nAddress:" +
strReturnedAddress;
Log.d("Current Address", latLongString);
}
else
{
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
Log.d("Current Address", latLongString);
}
private final LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle
extras) {
}
};
}
--
Thanks and Regards,
Ankit Kasliwal
[email protected]
+91-9300-940-136
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en