0

For a speed measuring app, I need to get the latitude and longitude updates after every four seconds. I've used the sleep function between the two calls to getLatitude and getLongitude functions. But printing it on screen shows the value remains the same i.e. lon1 and lon2, as doubles (14 places after the decimal) have exactly the same value and hence speed too is shown 0.

How to get Latitude and Longitude after fixed intervals of time (4 secs here)? Much thanks in advance.

   locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        locationListener = new LocationListener()
        {
            @Override
            public void onLocationChanged(Location location) {
                double r = 6371;
                double lat1 = location.getLatitude();
                double lon1 = location.getLongitude(); // To get initial longitude
                SystemClock.sleep(4000);              /////This seems ineffective.
                double lat2 = location.getLatitude();
                double lon2 = location.getLongitude(); // and here
                double dlat = (lat2-lat1)*22/7/180;
                double dlon = (lon2-lon1)*22/7/180;
             
                textView.append("lon1:"+lon1+" lon2:"+lon2+" Kmph\n");
                lat1 = lat1*22/7/180;
                lat2 = lat2*22/7/180;
                double h = ((1-Math.cos(dlat))/2) + Math.cos(lat1)*Math.cos(lat2)*(1-Math.cos(dlon))/2;
                double d = 2**Math.asin(Math.sqrt(h));
                double sp = d/3;
                textView.append(" "+ sp + " Kmph\n");
            }

            

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.INTERNET
                },10);
                return;
            }
        }else {
            configureButton();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 10:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    configureButton();
                return;
        }
    }

    private void configureButton() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                locationManager.requestLocationUpdates("gps", 4000, 0, locationListener);
            }
        });

    }
}
Mani
  • 1
  • 1
  • can you look at this just change 30 to 4 http://stackoverflow.com/questions/39314901/getting-latitude-and-longitude-in-30-seconds – Saveen Nov 08 '16 at 16:42
  • Thanks but this isn't helping in retrieving the current and previous latitude and longitude values. – Mani Nov 09 '16 at 07:35
  • it helpful for retrieving current values but why you need previous values ? – Saveen Nov 09 '16 at 08:36
  • I want to get the values lat1 and lon1; then after a delay of say 4 seconds two more such values lat2 and lon2; Then I am putting them in the haversine formula ( it gives the distance between two points whose latitudes and longitudes are known) and then dividing distance by time (4 secs here) to get the speed. – Mani Nov 09 '16 at 10:29
  • then simply save that lat lng somewhere then use it – Saveen Nov 09 '16 at 10:33
  • did it! it's there in the code.. I used sleep function to make it wait for 4 seconds. double lon1 = location.getLongitude(); // To get initial longitude SystemClock.sleep(4000); /////This seems ineffective. double lat2 = location.getLatitude(); double lon2 = location.getLongitude(); // and here But this doesn't work . the values of lon1 and lon2 are same, even when you are travelling. – Mani Nov 09 '16 at 12:50
  • somewhere where? I did save it in lat1 and lon1. But when I tried displaying the longitudes. I found lon1 = lon 2 !!! Even though there is a sleep of 4 seconds in between them. – Mani Nov 09 '16 at 13:43

1 Answers1

0

Hey i'm not sure but you are looking something like this Hope this will help you.

  float oldlat, oldlng;
  float currentLat, currentLng;


  // Use this in location changed 
  currentLat = (float) location.getLatitude();
  currentLng = (float) location.getLongitude();

   final Handler handler = new Handler();
   handler.postDelayed(new Runnable() {
   @Override
   public void run() {

        oldlat = (float) location.getLatitude();
        oldlng = (float) location.getLongitude();

   }
   }, 4000);
Saveen
  • 4,120
  • 14
  • 38
  • 41