I want to create a MapView application which shows my current location's latitude and longitude. After getting the current location, get the name of the location. Any help please?
5 Answers
With android it is actually pretty easy in order to get the location from the GPS Service. Use the LocationManager the easiest way to do it
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
ll);
.....A Spagehtti CODE GOES HERE.....
class CustomLocationListener implements LocationListener{ ............
public void onLocationChanged(Location argLocation) {
if(location != null) {
int latitude=(int)(argLocation.getLatitude()*1E6);
int longitude=(int)(argLocation.getLongitude()*1E6);
}
} ........ }
might also check Location Android API and Android Development

- 6,704
- 9
- 49
- 89
Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> add;
try
{
add = geo.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
if (add.size() > 0)
{
addl1=add.get(0).getAddressLine(0);
addl2=add.get(0).getAddressLine(1);
addl3=add.get(0).getAddressLine(2);
}
}
you can try this, for getting location name by put it in to the onLocationChanged.

- 31,955
- 11
- 77
- 132

- 1,720
- 13
- 26
Please refer the link: http://developer.android.com/guide/topics/location/obtaining-user-location.html
For getting location name, you should use GeoCoding.

- 9,379
- 14
- 55
- 94
In addition to test this using an emulator only you might need to set the location from outside, see: How to emulate GPS location in the Android Emulator?
Or even use a MockLocationProvider / LocationManager.setTestProvider.. for Unit testing.
Oh and by the way: If you already use a MapView, you might also be interest in using a MyLocationOverlay. It will display your current location on the Map. And by subclassing it, you can also hook the onLocationChanged method to plug in your custom location code that should run once the location changes.
use following code it may help you to find current city
Geocoder geocoder=new Geocoder(getBaseContext(),Locale.getDefault());
try
{
String city="";
List<Address> addresses= geocoder.getFromLocation(
geoPoint.getLatitudeE6() / 1E6,
geoPoint.getLongitudeE6() / 1E6,
1
);
if(addresses.size()>0)
{
city+=addresses.get(0).getSubAdminArea();
}
}
catch (IOException e)
{
e.printStackTrace();
}