91

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Prabhu M
  • 3,534
  • 8
  • 48
  • 87
  • 1
    Does the accepted answer works perfectly? For me it is not working...!!! – Samitha Chathuranga May 01 '15 at 17:15
  • 3
    Unfortunately the accepted answer is wrong :-( Amazing that it got 28 upvotes even though it doesn't answer the question correctly! – David Wasser May 08 '15 at 13:23
  • @DavidWasser Appreciate your valuable comments. And do you think that there is actually no any method to get the location details without using internet or gps? (other than by using that cell broadcast messages) – Samitha Chathuranga Jun 14 '15 at 06:58
  • 2
    @SamithaChathuranga Think about this: if you have no GPS and you have no Internet, where are you going to get this information? You can get the cell ID information from the network. This gives you only the ID. You would then need a way to map the ID to an actual coordinate or name. You could always build your own cell ID database and include it in your application, but it would take you an awful lot of work to collect all that data and the mapping isn't static: it changes as mobile operators install new cell towers and renumber their networks. Basically, the practical answer is "no". – David Wasser Jun 14 '15 at 19:52
  • @DavidWasser Thanks for the reply. I also had a big surprise how to get the location without internet or gps. But including stackoverflow in many forums this is said to be possible. That's why I thought it will be possible. Seems a lot of people are misguided too. – Samitha Chathuranga Jun 15 '15 at 03:21
  • Instead of Using GPS_PROVIDER you can use NETWORK_PROVIDER.It will show the user location based network provider.It gives user location approximately if you need accurate user location you need to use GPS_PROVIDER – Vishwa Jul 15 '15 at 12:24

8 Answers8

88

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.

Ian
  • 3,500
  • 1
  • 24
  • 25
  • 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 Jul 14 '11 at 16:40
  • 1
    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. – Ian Jul 15 '11 at 14:10
  • 2
    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? – Manann Sseth May 19 '14 at 06:06
  • 2
    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..... – pareshm Jan 22 '15 at 06:58
  • 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. – Samitha Chathuranga Apr 05 '15 at 12:48
  • 1
    Is it possible to get the co-ordinates without internet? In some of the devices it seems not working. – iammilind Apr 21 '15 at 12:12
  • @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 May 01 '15 at 17:18
  • 21
    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). – David Wasser May 08 '15 at 13:21
  • @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? – Samitha Chathuranga Jun 15 '15 at 03:27
  • @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. – David Wasser Jun 15 '15 at 07:04
  • @DavidWasser Can u give me a link where it is downloadable in small chunks? (I assume small chunks are free to download) – Samitha Chathuranga Jun 15 '15 at 18:22
  • @DavidWasser I found this site where Cell data are stored and updated continuously. http://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? – Samitha Chathuranga Jun 16 '15 at 04:07
  • 1
    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 Jun 16 '15 at 07:43
  • 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. – David Wasser Jun 16 '15 at 07:44
  • @Ian I tried this approach but still am unable to retrieve the location. Here: http://stackoverflow.com/q/41206885/6144372 Please help. – Hammad Nasir Dec 19 '16 at 09:39
19

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" />
Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
hwrdprkns
  • 7,525
  • 12
  • 48
  • 69
  • What I am thinking is, Is it possible to get current location using mobile network – Prabhu M Jul 14 '11 at 14:12
  • You need to read the docs. Learn the differences between `PASSIVE_PROVIDER`, `NETWORK_PROVIDER`, and `GPS_PROVIDER`. – hwrdprkns Jul 14 '11 at 14:16
  • 1
    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 Jul 14 '11 at 16:46
  • 1
    @Ian How to get the new or fresh location? – Archie.bpgc Mar 27 '13 at 12:37
  • @Archie.bpgc see my post. Once you've requestedLocationUpdates, OnLocationChanged() will be called when the device has received a new location. – Ian Mar 28 '13 at 21:31
  • @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 May 01 '15 at 17:28
  • @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? – Samitha Chathuranga May 03 '15 at 12:14
  • 7
    Can we get location if gps is off – Prasad Nov 23 '15 at 06:19
5

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.
mattm
  • 5,851
  • 11
  • 47
  • 77
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;
    }
}
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
Makvin
  • 3,475
  • 27
  • 26
3

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

Mahendran Candy
  • 1,114
  • 17
  • 17
2

Have you take a look Google Maps Geolocation Api? Google Map Geolocation

This is simple RestApi, you just need POST a request, the the service will return a location with accuracy in meters.

halfer
  • 19,824
  • 17
  • 99
  • 186
raditya gumay
  • 2,951
  • 3
  • 17
  • 24
-1

It appears that it is possible to track a smart phone without using GPS.

Sources:

Primary: "PinMe: Tracking a Smartphone User around the World"

Secondary: "How to Track a Cellphone Without GPS—or Consent"

I have not yet found a link to the team's final code. When I do I will post, if another has not done so.

halfer
  • 19,824
  • 17
  • 99
  • 186
-2

You can use TelephonyManager to do that .