Get current location of user in Android without using GPS or internet
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:
- 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.
- 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"
Comments
-
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 almost 13 yearsWhat I am thinking is, Is it possible to get current location using mobile network
-
hwrdprkns almost 13 yearsYou need to read the docs. Learn the differences between
PASSIVE_PROVIDER
,NETWORK_PROVIDER
, andGPS_PROVIDER
. -
Ian almost 13 yearswhat 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 almost 13 yearsAlthough 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 almost 13 yearsplease 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 about 11 years@Ian How to get the new or fresh location?
-
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 almost 10 yearsHey @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 over 9 yearsHey @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 about 9 yearsas 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 about 9 yearsIs it possible to get the co-ordinates without internet? In some of the devices it seems not working.
-
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 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 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 about 9 yearsThis 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 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 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 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 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 almost 9 yearsGood 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 almost 9 yearsThe 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 over 8 yearsCan we get location if gps is off
-
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 almost 5 yearsHow can you use it?