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

8

92

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.

Jodee answered 14/7, 2011 at 14:1 Comment(6)
Does the accepted answer works perfectly? For me it is not working...!!!Conflagration
Unfortunately the accepted answer is wrong :-( Amazing that it got 28 upvotes even though it doesn't answer the question correctly!Bigname
@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)Conflagration
@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".Bigname
@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.Conflagration
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_PROVIDERCalvities
C
89

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.

Casady answered 14/7, 2011 at 14:55 Comment(15)
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.Casady
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.Casady
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?Beuthen
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.....Chancy
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.Conflagration
Is it possible to get the co-ordinates without internet? In some of the devices it seems not working.Wilburn
@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.Conflagration
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).Bigname
@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?Conflagration
@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.Bigname
@DavidWasser Can u give me a link where it is downloadable in small chunks? (I assume small chunks are free to download)Conflagration
@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?Conflagration
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.Bigname
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.Bigname
@Casady I tried this approach but still am unable to retrieve the location. Here: https://mcmap.net/q/242373/-unable-to-retrieve-location-using-39-locationmanager-network_provider-39-even-when-wifi-is-enabled-in-device-running-android-m/6144372 Please help.Hullabaloo
N
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" />
Nnw answered 14/7, 2011 at 14:5 Comment(8)
What I am thinking is, Is it possible to get current location using mobile networkJodee
You need to read the docs. Learn the differences between PASSIVE_PROVIDER, NETWORK_PROVIDER, and GPS_PROVIDER.Nnw
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.Casady
@Casady How to get the new or fresh location?Acidimeter
@Acidimeter see my post. Once you've requestedLocationUpdates, OnLocationChanged() will be called when the device has received a new location.Casady
@Nnw 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.Conflagration
@Casady 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?Conflagration
Can we get location if gps is offGossip
N
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.
Notification answered 3/9, 2017 at 16:43 Comment(0)
C
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;
    }
}
Connally answered 14/12, 2016 at 9:45 Comment(0)
B
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

Batfowl answered 16/4, 2016 at 6:54 Comment(0)
D
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.

Disentomb answered 11/6, 2016 at 9:8 Comment(0)
N
-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.

Notional answered 31/5, 2018 at 21:41 Comment(0)
S
-2

You can use TelephonyManager to do that .

Stewpan answered 2/4, 2019 at 9:56 Comment(1)
How can you use it?Smutch

© 2022 - 2024 — McMap. All rights reserved.