I need the correct way of getting latitude and longitude in Android programmatically. I viewed different sites and forums but still I couldn't get the correct one. The program should support for all the Android versions. I use wifi to get my device connected to net.
Dont Worry, After scratching my head over few days, I got code working in over a few lines to get longitude and latitude values... I think this will help things go better, Thanks for your suggestions!!!!
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.length();i>=0;i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;}