Get current location of user in Android without using GPS or internet

152,103

Solution 1

What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.

http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.

After you get done with most of the code in OnCreate(), add this:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      makeUseOfNewLocation(location);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.

Solution 2

By getting the getLastKnownLocation you do not actually initiate a fix yourself.

Be aware that this could start the provider, but if the user has ever gotten a location before, I don't think it will. The docs aren't really too clear on this.

According to the docs getLastKnownLocation:

Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider.

Here is a quick snippet:

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import java.util.List;

public class UtilLocation {
    public static Location getLastKnownLoaction(boolean enabledProvidersOnly, Context context){
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location utilLocation = null;
        List<String> providers = manager.getProviders(enabledProvidersOnly);
        for(String provider : providers){

            utilLocation = manager.getLastKnownLocation(provider);
            if(utilLocation != null) return utilLocation;
        }
        return null;
    }
}

You also have to add new permission to AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Solution 3

No, you cannot currently get location without using GPS or internet.

Location techniques based on WiFi, Cellular, or Bluetooth work with the help of a large database that is constantly being updated. A device scans for transmitter IDs and then sends these in a query through the internet to a service such as Google, Apple, or Skyhook. That service responds with a location based on previous wireless surveys from known locations. Without internet access, you have to have a local copy of such a database and keep this up to date. For global usage, this is very impractical.

Theoretically, a mobile provider could provide local data service only but no access to the internet, and then answer location queries from mobile devices. Mobile providers don't do this; no one wants to pay for this kind of restricted data access. If you have data service through your mobile provider, then you have internet access.

In short, using LocationManager.NETWORK_PROVIDER or android.hardware.location.network to get location requires use of the internet.

Using the last known position requires you to have had GPS or internet access very recently. If you just had internet, presumably you can adjust your position or settings to get internet again. If your device has not had GPS or internet access, the last known position feature will not help you.

Without GPS or internet, you could:

  1. Take pictures of the night sky and use the current time to estimate your location based on a star chart. This would probably require additional equipment to ensure that the angles for your pictures are correctly measured.
  2. Use an accelerometer to track location starting from a known position. The accumulation of error in this kind of approach makes it impractical for most situations.

Solution 4

boolean gps_enabled = false;
boolean network_enabled = false;

LocationManager lm = (LocationManager) mCtx
                .getSystemService(Context.LOCATION_SERVICE);

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Location net_loc = null, gps_loc = null, finalLoc = null;

if (gps_enabled)
    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (gps_loc != null && net_loc != null) {

    //smaller the number more accurate result will
    if (gps_loc.getAccuracy() > net_loc.getAccuracy()) 
        finalLoc = net_loc;
    else
        finalLoc = gps_loc;

        // I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)

} else {

    if (gps_loc != null) {
        finalLoc = gps_loc;
    } else if (net_loc != null) {
        finalLoc = net_loc;
    }
}

Solution 5

Here possible to get the User current location Without the use of GPS and Network Provider.

1 . Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"

2.Click Here For Your Reference

Share:
152,103
Prabhu M
Author by

Prabhu M

I am an Android Developer from Bangalore, India.

Updated on April 02, 2020

Comments

  • Prabhu M
    Prabhu M about 4 years

    Is it possible to get the current location of user without using GPS or the internet? I mean with the help of mobile network provider.

  • Prabhu M
    Prabhu M almost 13 years
    What I am thinking is, Is it possible to get current location using mobile network
  • hwrdprkns
    hwrdprkns almost 13 years
    You need to read the docs. Learn the differences between PASSIVE_PROVIDER, NETWORK_PROVIDER, and GPS_PROVIDER.
  • Ian
    Ian almost 13 years
    what do you mean? From the LocationManager documentation for NETWORK_PROVIDER: "This provider determines location based on availability of cell tower and WiFi access points." One can imagine that without wifi, the phone will default to cell towers.
  • Ian
    Ian almost 13 years
    Although this is ideal, I would check the time of the last location using getTime(). Then, it is just a matter of deciding how old you will allow this to be. If it is too old, then get a new position using my post.
  • Ian
    Ian almost 13 years
    please remember to check out strategies on battery preservation. Also, depending on your application, the PASSIVE_PROVIDER and using getLastKnownLocation may save you a lot of work and battery if other applications are using the antennas.
  • Archie.bpgc
    Archie.bpgc about 11 years
    @Ian How to get the new or fresh location?
  • Ian
    Ian about 11 years
    @Archie.bpgc see my post. Once you've requestedLocationUpdates, OnLocationChanged() will be called when the device has received a new location.
  • Manann Sseth
    Manann Sseth almost 10 years
    Hey @Ian, I've used same code.. But in my case, onLocationChanged() listener method never called. I used requestLocationUpdates on onResume() method. still it never called.. So what had been issue there?
  • pareshm
    pareshm over 9 years
    Hey @lan or any other person who can help me out i implemented above code but onLocationChanged method neved gets called what would be the problem can any one help me out please.....
  • Samitha Chathuranga
    Samitha Chathuranga about 9 years
    as I think onLocationChanged will run only when a location change occurs and practically when testing, this never happens. And even the question is to get the location directly but not when some even occurs. So this answer is not giving any help.
  • iammilind
    iammilind about 9 years
    Is it possible to get the co-ordinates without internet? In some of the devices it seems not working.
  • Samitha Chathuranga
    Samitha Chathuranga about 9 years
    @lan Can u please answer my problem. And anybody who succeeded with this solution, please mention it. Because for me this is not working and I want to know whether it is a problem with my phone which I tested my app.
  • Samitha Chathuranga
    Samitha Chathuranga about 9 years
    @hwrdprkns to call getLastKnownLocation and successfully get a location, we should have a previous-recorded-last-location. But the question is asked to get the location data directly without a previous knowledge. And what I too need is that. So what is your idea on this.
  • Samitha Chathuranga
    Samitha Chathuranga about 9 years
    @Ian As also u said "OnLocationChanged() will be called when the device has received a new location." But what if the location was not changed and there is also not a "last known location", what will happen?
  • David Wasser
    David Wasser about 9 years
    This answer is wrong :-( OP asked how to get location without using an Internet connection. LOCATION_PROVIDER needs to use the Internet to access servers to resolve a collection of Cell-ID and WIFI MAC addresses into a geo coordinate. If you have no Internet connection you will not be able to get a coordinate. If you tell me it works for you, it means that either you have an Internet connection, or you had an Internet connection (location API downloads and caches some information about the area around you), or the location information is old (and therefore inaccurate).
  • Samitha Chathuranga
    Samitha Chathuranga almost 9 years
    @DavidWasser Is there any open database where the Cell id and location coordinates are mapped which we can download? Or Google should be using such a one and is there a way to download their that database?
  • David Wasser
    David Wasser almost 9 years
    @SamithaChathuranga no, there isn't. Google has a database but it is large and it belongs to them so it isn't freely available and it is dynamic and it isn't downloadable except in small chunks. Google wants you to use their connected services.
  • Samitha Chathuranga
    Samitha Chathuranga almost 9 years
    @DavidWasser Can u give me a link where it is downloadable in small chunks? (I assume small chunks are free to download)
  • Samitha Chathuranga
    Samitha Chathuranga almost 9 years
    @DavidWasser I found this site where Cell data are stored and updated continuously. opencellid.org/#action=database.downloadDatabase We can register free and get an api key and access the download site and download files in .csv format. What do u think?
  • David Wasser
    David Wasser almost 9 years
    Good luck with that. The data is spotty. There is some good data and some old data and a lot of missing data. If you want to give it a try, go ahead. It all depends on what you are trying to do.
  • David Wasser
    David Wasser almost 9 years
    The Google data is downloadable in small chunks. That's the way Google does it. But their data is not freely available, as I told you.
  • Prasad
    Prasad over 8 years
    Can we get location if gps is off
  • Hammad Nasir
    Hammad Nasir over 7 years
    @Ian I tried this approach but still am unable to retrieve the location. Here: stackoverflow.com/q/41206885/6144372 Please help.
  • CoolMind
    CoolMind almost 5 years
    How can you use it?