5

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
Ankit
  • 1,148
  • 3
  • 15
  • 31
  • Have you enabled the "Use GPS satellites" preference in the "Location and security" system preferences? – Reuben Scratton Dec 07 '10 at 12:19
  • Hi Reuben, of course i have enabled it, see the problem i m facing is though i m trying to fetch through GPS_PROVIDER, it requires Network Operator's help, which is strange. – Ankit Dec 07 '10 at 12:23

4 Answers4

10

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.

Community
  • 1
  • 1
Stéphane
  • 6,920
  • 2
  • 43
  • 53
  • thanks stephen.. actually phone was not catching signals inside my office.. and i was really new to android so couldn't get this.. and whenever i was inserting my sim into device, it was fetching as i had mentioned both LocationManager.GPS_PROVIDER and LocationManager.NETWORK_PROVIDER.. – Ankit Feb 11 '11 at 13:26
0

Here is my main activity with a button and I have set an onClickListner on it to toggle my GPS from OFF state to ON and there is no need of any permission to do this.
Tested with Android 2.2 and working fine for me.

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GPSONOFFActivity extends Activity {
/** Called when the activity is first created. */
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button)findViewById(R.id.button1);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            toggleGPS(true);                
        }
    });
}

private void toggleGPS(boolean enable) {
    String provider = Settings.Secure.getString(getContentResolver(), 
        Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

    if(provider.contains("gps") == enable) {
        return; // the GPS is already in the requested state
    }

    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", 
        "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3"));
    this.sendBroadcast(poke);
 }
}
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
  • I tried but not worked for me in my device which has OS 4.0.3. Is there any permission i need to mention in manifest, as it is not showing any error so i guess nothing is happening. – Ankit May 25 '12 at 05:37
0

I think one problem you are having may be the fact that there aren't any "maps" built into Android. The maps are loaded from Google's servers on the fly as a phone needs them. For example, why load a map of the entire country when you only need a map of a neighborhood? That's the theory behind it. It grabs your location, sends it to Google's server, and it dishes you back the map it thinks you need. Of course, if you downloaded all the maps(which I heard is possible, but I'm not totally sure) onto your device, then this probably wouldn't be the case...

EDIT: Google does not allow downloading their maps. (Thanks to Austyn for info).

Brandon
  • 1,373
  • 2
  • 12
  • 19
  • Google disallows full caching of their map tiles. To do this you would need to download an alternative mapping application (http://wiki.openstreetmap.org/wiki/Android) that allows local storage of map tiles. – Austyn Mahoney Feb 08 '11 at 21:22
0

This might be outdated, but do you have any idea what the error is?

I'm getting the error as, "error getting cell location info".

I found the source code that lead to this error.

private void requestRefLocation(int flags)
{
    TelephonyManager phone = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    if (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)
    {
        GsmCellLocation gsm_cell = (GsmCellLocation) phone.getCellLocation();
        if ((gsm_cell != null)
                && (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)
                && (phone.getNetworkOperator() != null)
                && (phone.getNetworkOperator().length() > 3))
        {
            int type;
            int mcc = Integer.parseInt(phone.getNetworkOperator().substring(0, 3));
            int mnc = Integer.parseInt(phone.getNetworkOperator().substring(3));
            int networkType = phone.getNetworkType();
            if (networkType == TelephonyManager.NETWORK_TYPE_UMTS
                    || networkType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || networkType == TelephonyManager.NETWORK_TYPE_HSUPA
                    || networkType == TelephonyManager.NETWORK_TYPE_HSPA)
            {
                type = AGPS_REF_LOCATION_TYPE_UMTS_CELLID;
            }
            else
            {
                type = AGPS_REF_LOCATION_TYPE_GSM_CELLID;
            }
            native_agps_set_ref_location_cellid(type, mcc, mnc, gsm_cell.getLac(), gsm_cell.getCid());
        }
        else
        {
            Log.e(TAG, "Error getting cell location info.");
        }
    } else {
        Log.e(TAG, "CDMA not supported.");
    }
}

Notice the check statement

if ((gsm_cell != null)
            && (phone.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM)
            && (phone.getNetworkOperator() != null)
            && (phone.getNetworkOperator().length() > 3))
    {

It somehow leads to the problem you're facing. Of course, by connecting to a WIFI network, you will still get your location as it's using a different way to get it. I hope this answer your question.

Creniale
  • 238
  • 3
  • 16