Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 558
Prabhu M Avatar asked Jul 14 '11 14:07

Prabhu M


People also ask

How can I find my mobile location without Internet?

Finding Android Phones without Internet. It is a pretty easy and simple method which involves installing an app in your phone known as “Where's my droid”. You need to install this app in your phone and give all the corresponding permissions for GPS.

What are the methods used to get user location in maps in Android?

If your app needs to access the user's location, you must request permission by adding the relevant Android location permissions to your app. Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API.


2 Answers

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.

like image 127
Ian Avatar answered Oct 08 '22 12:10

Ian


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" /> 
like image 21
hwrdprkns Avatar answered Oct 08 '22 13:10

hwrdprkns