1

I have searched all the links, still i am not able to get a continuous data from gps as my location changes. I have to press the button every time to get the location. What i want is, that map keeps on updating the location with 1 button click and returns latitude, longitude, speed, distance and other information every time.

Here's my code

public class Gps extends Activity {

 private EditText lat,lon,speed,acc,alt,t,add;
 private ProgressBar progress;
 private GoogleMap googleMap;
 private LocationManager locManager;
 private LocationListener locListener = new MyLocationListener();

 private boolean gps_enabled = false;
 private boolean network_enabled = false;

 public Looper mLooper;

 Geocoder geocoder;
 List<Address> addresses;


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);

    lat = (EditText) findViewById(R.id.lat);
    lon = (EditText) findViewById(R.id.lon);
    acc = (EditText) findViewById(R.id.acc);
    speed = (EditText) findViewById(R.id.speed);
    alt = (EditText) findViewById(R.id.alt);
    t = (EditText) findViewById(R.id.time);
    add = (EditText) findViewById(R.id.add);
    progress = (ProgressBar) findViewById(R.id.p1);
    progress.setVisibility(View.GONE);

    //buttonGetLocation.setOnClickListener(this);

    locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
    //googleMap.setMyLocationEnabled(true);

}
public void onClickb(View v) {
    // TODO Auto-generated method stub

    new find().run();
    //Connect c = new Connect();
    //c.execute();
}




 class MyLocationListener implements LocationListener
 {

    public void onLocationChanged(Location location) 
    {
        if (location != null)
        {

            // This needs to stop getting the location data and save the battery power.
            locManager.removeUpdates(this);


            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            double altitiude =location.getAltitude();
            double accuracy = location.getAccuracy();
            double time = location.getTime();
            String spmsg = null,s=null; 


            try
            {       
                GetCompleteAddressString ga = new GetCompleteAddressString();
                s = ga.getAddress(latitude, longitude); 
                //Toast.makeText(Gps.this,s, Toast.LENGTH_LONG).show();                 
                Float thespeed=location.getSpeed();
                Float sp=(thespeed/1000);
                spmsg="Speed : "+sp;
                //Toast.makeText(Gps.this,spmsg, Toast.LENGTH_LONG).show();                             
            }
            catch(Exception e)
            {
                Toast.makeText(Gps.this,e.toString(), Toast.LENGTH_LONG).show();
            }

            lat.setText("Longitude : "+longitude);
            lon.setText("Latitude : "+ latitude);
            acc.setText("Accuracy : "+ accuracy);
            t.setText("Time : "+ time);
            speed.setText(spmsg);
            alt.setText("Altitude : " +altitiude);
            add.setText(s);
            progress.setVisibility(View.GONE);

            CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(latitude, longitude)).zoom(14).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

            // adding marker
            googleMap.addMarker(marker);
           // Looper.getMainLooper().quit();
        }
        //mLooper.quit();
        //Looper.myLooper().quit();
}

    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }



}
public class find extends Thread
 {

    public find()
    {
        locListener = new MyLocationListener();

        try 
        {
            gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } 
        catch (Exception ex) 
        {}

        try 
        {
            network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }
        catch (Exception ex)
        {}

        // don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
        {
            Toast.makeText(getApplicationContext(), "Sorry, location is not determined. Please enable location providers", Toast.LENGTH_LONG).show();
            progress.setVisibility(View.GONE);

        }   

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        //super.run();
        //Looper.prepare();
        /*if(Looper.myLooper() == null) { // check already Looper is associated or not.
               Looper.prepare(); // No Looper is defined So define a new one
            }
        */


        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 0, locListener);
            }
            if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5, 0, locListener);
            }
        //Looper.loop();
    }

 }

}
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
dvs
  • 644
  • 5
  • 14
  • you can use the service for the same – Anjali Jan 08 '15 at 09:10
  • 2
    you need to create onOLocationChangeListener. i recommend you take a look on this: http://gabesechansoftware.com/location-tracking/ – Maor Hadad Jan 08 '15 at 09:10
  • You will have no problems using Google's guide: https://developer.android.com/guide/topics/location/strategies.html – shkschneider Jan 08 '15 at 09:22
  • a note about the 2 `if` loops in your `run()` and what may help in obtaining location [in this answer](http://stackoverflow.com/questions/23962769/android-gps-incorrect-location-data-on-query/24074771#24074771) – Pararth Jan 08 '15 at 09:22

2 Answers2

4

You are causing it to stop listening after the first location.

public void onLocationChanged(Location location) 
{
    if (location != null)
    {
        // This needs to stop getting the location data and save the battery power.
        locManager.removeUpdates(this); <-- REMOVE THIS LINE
Carnal
  • 21,744
  • 6
  • 60
  • 75
0

onOLocationChangeListener is there already doing this for you.

onLocationChanged(Location location)

Called when a new user location is known.

Example/Source code can be found here

Community
  • 1
  • 1
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35