0

I have implemented a simple app on my device that gets and displays your GPS latitude and longitude. However the co-ordinates donot change even when i move about in my building. Here is my implementation:

foodActivity.java

public class foodActivity extends Activity {

LocationListener mlocListener;
Location location;
String provider;
private double latitude = 0;
private double longitude = 0;
private Handler mHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.activity_food);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_food);


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

    Criteria criteria = new Criteria();
    provider = mlocManager.getBestProvider(criteria, false);

    if (provider != null && !provider.equals("")) {

        location = mlocManager.getLastKnownLocation(provider);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(provider, 0, 0, mlocListener);

        if (location != null) {
            mHandler = new Handler();
            mHandler.postDelayed(updateTask, 0);

        } else {
            Toast.makeText(getBaseContext(), "Location can;t be retrieved", Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(getBaseContext(), "No Provider found", Toast.LENGTH_SHORT).show();
    }
}
/* Class My Location Listener */

public class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub

        TextView lats = (TextView) findViewById(R.id.tv_latitude);
        TextView longs = (TextView) findViewById(R.id.tv_longitude);

        latitude = loc.getLatitude();
        longitude = loc.getLongitude();

        lats.setText("Latitude :" + latitude);
        longs.setText("Longitude :" + longitude);

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
    }

}

public Runnable updateTask = new Runnable(){
    public void run(){
        mlocListener.onLocationChanged(location);
        mHandler.postDelayed(updateTask,50000);
    }
};
}

activity_food.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.hkuapp.foodActivity">

<TextView
    android:text="Hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_latitude"
    android:layout_below="@+id/tv_longitude"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="32dp" />

<TextView
    android:text="Yo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_longitude"/>

</RelativeLayout>
stud91
  • 1,854
  • 6
  • 31
  • 56
  • 2
    "...in my building" GPS doesn't work too good in buildings, depending on materials building is made of. Code seems OK, so try outside. – weston Jun 19 '14 at 08:18
  • Well I have spotted something; your `Criteria` is empty. http://developer.android.com/reference/android/location/Criteria.html#Criteria() `The new object will have no requirements on accuracy, power, or response time` – weston Jun 19 '14 at 08:21
  • If you work in a dangerous area or sun burn easy you can also run in emulator and [simulate the GPS](http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator) :) – weston Jun 19 '14 at 08:25
  • @weston i went outside..moved around for 100metres and so but no change in the co-ordinates – stud91 Jun 19 '14 at 08:38

2 Answers2

0

In this method mlocManager.requestLocationUpdates(provider, 0, 0, mlocListener); the second parameter is time interval between location updates, in milliseconds. Try to change it to 1000 (1 second).

Roman Black
  • 3,501
  • 1
  • 22
  • 31
0

From what I see in your code:

  1. You only update your location every 50 seconds
  2. You said that you tested it indoors, so your device probably doesn't sense any location changes.
ionutioio
  • 218
  • 1
  • 6