3

i have a situation where i need to use GPS technique.

i need to find the distance between two points when the person is walking.

When the person starts walking he will click on Start button and when he stops he clicks on stop button

after this it should show him

1.time taken 2.Distance travelled in Kms. 3.from where to where(places name) eg: a to b

Do i need to have google maps for this?

I saw the code here link to get the current location which gives me latitude longitude.

please help how to go with this

**

Edited:

**

This is the code i am using

private EditText editTextShowLocation;
    private Button buttonGetLocation;
    private ProgressBar progress;

    private LocationManager locManager;
    private LocationListener locListener = new MyLocationListener();

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);

        progress = (ProgressBar) findViewById(R.id.progressBar1);
        progress.setVisibility(View.GONE);

        buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);
        buttonGetLocation.setOnClickListener(this);

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

    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        // exceptions will be thrown if provider is not permitted.
        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) {
            AlertDialog.Builder builder = new Builder(this);
            builder.setTitle("Attention!");
            builder.setMessage("Sorry, location is not determined. Please enable location providers");
            builder.setPositiveButton("OK", this);
            builder.setNeutralButton("Cancel", this);
            builder.create().show();
            progress.setVisibility(View.GONE);
        }

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

    class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                // This needs to stop getting the location data and save the battery power.
                locManager.removeUpdates(locListener); 

                String londitude = "Londitude: " + location.getLongitude();
                String latitude = "Latitude: " + location.getLatitude();
                String altitiude = "Altitiude: " + location.getAltitude();
                String accuracy = "Accuracy: " + location.getAccuracy();
                String time = "Time: " + location.getTime();

                editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + time);
                progress.setVisibility(View.GONE);
            } 
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

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

        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which == DialogInterface.BUTTON_NEUTRAL){
            editTextShowLocation.setText("Sorry, location is not determined. To fix this please enable location providers");
        }else if (which == DialogInterface.BUTTON_POSITIVE) {
            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    }

It is showing the Logitude Latitude which i am entering from emulator control.

In this i am manually entering the details of longitude and latitude by going to window->showview->other->emulator control for testing in the emulator but what i need is i will have two edittext where i enter the place name(A) and (B)

it should give me the distance

please help

Goofy
  • 6,098
  • 17
  • 90
  • 156
  • small suggestion.. take the code out of the onclick and into a named function. It's a small thing, but it makes the code heaps easier to read if you know what the method is supposed to be doing. – baash05 Mar 12 '12 at 22:12

5 Answers5

2

try using Google Distance Matrix Api

https://developers.google.com/maps/documentation/distancematrix/

Satish
  • 56
  • 3
0

You can find out the distance between two locations(in terms of latitude and longitude) by making use of Spherical Trigonometry

Coming to time make use of simple date objects and compare the startTime and endTime.

(OR)

You can get approximate distance using below code

double distance;  

Location locationA = new Location("point A");  

locationA.setLatitude(latA);  
locationA.setLongitude(lngA);  

Location locationB = new Location("point B");  

locationB.setLatitude(latB);  
LocationB.setLongitude(lngB);  

distance = locationA.distanceTo(locationB); 
Sai mukesh
  • 712
  • 3
  • 9
  • don't go so far as spherical trig.. this is a runner, not a pilot.. Yeah it won't be quite perfect, with accuracy, but man it would be much easier. – baash05 Mar 12 '12 at 06:16
0

You can use currentTimeinMillis() to get your start and end time for your journey.

You can then use the formulas explained here to find the distance and lastly you will have to use a reverse geocoding service such as Nominatim to be able to get the address of a place from your GPS coordinates.

That being said, the distance formula will get you the distance between one point and the next, not the actual displacement. If this is not what you need, but rather you want the actual distance travelled you will need to calculate this value at a shorter interval.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

I suppose the question is one of walking..

Are you walking on the streets, or as the crow flies?

If it's streets, and your connected to the net, use google's api.. It calculates routing based on two points and returns XML.. Should be easy enough to figure out.

If it's crow flies.. well then, just do (a*a) + (b*b) = (c*c) this is by far the easier.. You could have your user tap for major turns.. Or you could keep a runnable running every 10 seconds from when they hit start, and plot the points. Still a*a+b*b=c*c but just a bunch of steps.

Of course you'd have to run it in a service.. And given the choice I'd go with that option. You could adjust the cycle time based on speed traveled. Faster would be smaller pauses. It requires less on your dataplan.


EDIT
Ah.. I see what you're looking for. Tis not what I thought you were asking for. Simplify.. convert lat/long to GPS. and then do simple math on the last point stored
void addDistance()
{
  newX = ...
  newY = ...
  deltaX = absolute(m_oldX - newX)
  deltaY = absolute(m_oldY = newY)
  m_distance += sqrt(deltaX^2 + deltaY^2);
  m_oldX = newX;
  m_oldY = newY;
}
void startOver()
{
  newX = ...
  newY = ...
  m_distance = 0;
  m_oldX = newX;
  m_oldY = newY;

}
baash05
  • 4,394
  • 11
  • 59
  • 97
  • please help me with this question http://stackoverflow.com/questions/9664587/using-gps-get-the-distance-a-person-has-walked – Goofy Mar 13 '12 at 17:00
0

For getting the distance between 2points(A to B) there is a function called distanceTo in android.

For e.g. double distance = startLocation.distanceTo(finishLocation)/1000;

For time taken as npinti said you can use currentTimeinMillis() or you can also use Timer and show it to user when he clicks on start button. Its just like stopwatch.

Edited

Place A - New York

Place B - Paris

In this case you first need to convert the string into Location(i.e you need latitude & longitude). For that you have use the concept of Geocoding.

List<Address> foundGeocode = null;
foundGeocode = new Geocoder(this).getFromLocationName("address here", 1);
 foundGeocode.get(0).getLatitude(); //getting latitude
 foundGeocode.get(0).getLongitude();//getting longitude

After that you can calculate the distance from the distanceTo method. Hope this will help....

Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • 2
    http://android-coding.blogspot.in/2011/07/get-latitude-and-longitude-from.html refer this link...nice explanation with demo for getting location from string..... – Scorpion Mar 12 '12 at 06:38
  • thanks man this is what i needed.But i also need to know whether i can acheive like this? i have two buttons and Start and Stop when he clicks on start we need to get the current location with lat and long and when i click on stop again i need to get the current location with lat and long and so that i can calulate the distance without entering the places name – Goofy Mar 12 '12 at 06:45
  • Yes you can achieve that. On click of the button you call the method for getting current location. That way you can do this. – Scorpion Mar 12 '12 at 09:41
  • please refer to this question and help http://stackoverflow.com/questions/9664587/using-gps-get-the-distance-a-person-has-walked – Goofy Mar 12 '12 at 12:44