Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get location without internet in android, using only GPS

Tags:

android

I want to get location using GPS only. I don't want to use internet and GPRS in this application. My code is below; tell me where I'm wrong in this.

code:

package com.getlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class UseGps extends Activity {
    /** Called when the activity is first created. */
    private String provider;
    LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); /*
                                         * Use the LocationManager class to
                                         * obtain GPS locations
                                         */
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
         Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
            locationManager.requestLocationUpdates(provider, 61000, 250,
                mlocListener);
    } /* Class My Location Listener */
public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitude = "
                + loc.getLatitude() + "Longitude = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();
        Log.d("TAG", "Starting..");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Disabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */
like image 221
sam_k Avatar asked May 16 '11 05:05

sam_k


People also ask

Is it possible to get the location in Android using GPS?

Yes, it is possible to get the location in Android using GPS without an internet connection. You can track your location without needing an internet connection with any mapping app. It is reliable but having an internet connection will give more accurate results. You need to put the permission in the manifest file.

How do I Find my Android device without the GPS chip?

Per Find your device using Android Device Manager , you don't need the GPS chip turned on, but you do need to have either a 3G or wifi connection enabled. Google location services combines data from 3G (cell tower IDs), wifi (access point SSIDs/MAC from streetview surveys and other Android users) as well as GPS to derive a location.

What can I do without GPS or Internet?

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. Use an accelerometer to track location starting from a known position.

How can I track my location without GPS or Internet?

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.


2 Answers

Use this for only GPS Provider, it does not need GPRS.

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

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

You need to put the permission in manifest file.

like image 127
PravinCG Avatar answered Sep 19 '22 06:09

PravinCG


You don't need an internet connection to run GPS system in your mobile. GPS time synchronization does not require an Internet connection. But if you want to show the current location on google map, you may require internet connection.

Coming to you code everything looks fine for me.

Try this code in your activity.

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

LocationListener mlocListener = new YourLocationListener(getApplicationContext(), mobileNo, deviceId);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mlocListener);

and include this in androidmanifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
like image 42
Krishna Avatar answered Sep 22 '22 06:09

Krishna