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
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:
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.
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).
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);
}
}
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.