Skip to content Skip to sidebar Skip to footer

Android: Getting Latitude And Longitude

Is it possible to get the Lat & Lng without going outdoor? My current situation is that user would have to be outdoor in order to get their location , in which also takes quite

Solution 1:

Yes it is possible , take hint from following code:

try {
            gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        if (!gps_enabled && !network_enabled) {
             // show alert 
        }
        if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
        }
        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
        }

Solution 2:

Yes, It is Possible, Inside the door you are getting your network. so you can use LocationManager.NETWORK_PROVIDER for getting the GPS Co-Ordinates.

Please look at to this answer.

Solution 3:

It depends on the GPS Provider you are using, and signal availabiliy to fix the location, I think you are using GPS provider only, so it is not working in door, try using network provider also in consideration.

Also, use Last known location, by the time gps fixes location, there is a good blog to fetch location at earliest.

android-developers.blogspot.in/2011/06/deep-dive-into-location.html

Solution 4:

    LocationManager locManager;   
    Location location;
 double lat;
 double lng;
    try {
                gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            } catch (Exception ex) {
            }
            try {
                network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            } catch (Exception ex) {
            }

            if (!gps_enabled && !network_enabled) {
                 // show alert 
            }
            if (network_enabled) {
                  locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);                
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,1, locationListener);
                location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
             lat = location.getLatitude();
             lng = location.getLongitude();
            }
            if (gps_enabled) {
                locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);              
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1, locationListener);
                location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             lat = location.getLatitude();
             lng = location.getLongitude();
            }

Post a Comment for "Android: Getting Latitude And Longitude"