coderslay
coderslay

Reputation: 14370

How to Obtain Latitude and Longitude in Android

How to obtain Latitude and longitude from the mobile device if GPS is not available... My mobile has internet Connection through wifi and gprs connection.... Can we get the lats and long from this?

Upvotes: 2

Views: 3109

Answers (2)

Uday
Uday

Reputation: 6023

edited : Network and also GPS Provider Location

Yes we cam...try this

LocationManager manager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// additionally (you have to implement LocationListener)
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new LocationListener() ...)

After location you can use in locationlistener...

 @Override
    public void onLocationChanged(Location location) {                          
         Log.d("a","onLocationChanged: lat="+location.getLatitude());
         Log.d("a","onLocationChanged: lat="+location.getLongitude());                 
    }

hope it helps..

Upvotes: 1

Heiko Rupp
Heiko Rupp

Reputation: 30934

Yes you can. Have a look at the documentation

When you have a Location from the system. you can use methods getLongitude() and getLatitude()

For a full example have a look at this code - lines 122-124 to set up receiving updates and lines 201-209 to get the Location.

Upvotes: 0

Related Questions