Get Latitude and Longitude without inserting Sim Card and Start/Stop Gps from Application

Ankit picture Ankit · Dec 7, 2010 · Viewed 14.4k times · Source

I am trying to fetch Gps latitude and longitude, but i am having a query that whenever i try to fetch Lat & Long without sim card, it is not providing any info where as soon i insert my sim card, it provides me all information in desired manner.

LocationManager mlocManager = 
    (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                   0, 0, mlocListener);

you can see though i m using GPS_PROVIDER, it is not giving me Lat & Long without Network Operator help, can anybody tel me?


Another thing is How to start and stop GPS from an application means i wanna start gps after particular time and than as soon i get beslocation, i can turn it off.

Answer

Stéphane picture Stéphane · Feb 8, 2011

You don't need to have access to any network to get a GPS location on Android. But an internet connection speeds-up the location fix from about 5 minutes to 5 seconds (approximately)! This improvement is brought by Assisted-GPS (A-GPS), as opposed to standalone-GPS.

GPS phones (including Android devices) check if a network connection is available. If so, they download assistance data from and A-GPS server and use it to compute the location fix with the GPS chipset. If not, the GPS chipset has to download the data from a GPS satellite, which takes a long time (rate is 50 bits per second!). The process can also be sped up if a GPS location has been computed not too long ago.

So first:

  • check that you have added ACCESS_FINE_LOCATION to your manifest's permissions
  • check that GPS satellites are enabled in the phone's settings
  • check that you are not indoors (signals from satellites 20,000 km above do not like roofs and walls)

Then start your app and wait, there can be a few minutes before the first fix is available.

As for starting the GPS from your app, it seems possible (see How can I enable or disable the GPS programmatically on Android?), but I don't know how! At least you can download Tasker and program it to start/stop the GPS.