indira
indira

Reputation: 6687

How to get current location (latitude and longitude) in emulator using Android mapview?

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?

Upvotes: 2

Views: 9830

Answers (5)

Miral Dhokiya
Miral Dhokiya

Reputation: 1710

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.

Upvotes: 1

ram
ram

Reputation: 271

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();
}

Upvotes: 0

icyerasor
icyerasor

Reputation: 5242

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.

Upvotes: 0

Necronet
Necronet

Reputation: 6813

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

Upvotes: 1

Anju
Anju

Reputation: 9479

Please refer the link: http://developer.android.com/guide/topics/location/obtaining-user-location.html

For getting location name, you should use GeoCoding.

Upvotes: 0

Related Questions