2

I'm creating a riding app, it is a Uber like app. Using PHP for API to communicate Android and ios device. My backend code is almost done along with Android and ios app. Now I'm having problem to calculate realtime distance. If I use google distance matrix api, google return me distance that is not my real time distance. I mean google return based on route available. If I follow route suggested by google distance is okay for me but if I use different route how I can calculate distance? Is it possible to calculate realtime distance? If possible can you please give me an idea?

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
alaminio
  • 82
  • 2
  • 12

3 Answers3

0

Use this

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) ;
Aditay Kaushal
  • 1,021
  • 7
  • 17
0

// try like this , it will help you

CLLocation * Location1 = [[CLLocation alloc] initWithLatitude:latitude1 longitude:langitude1];
CLLocation * Location2 = [[CLLocation alloc] initWithLatitude:latitude2 longitude:langitude2];;


CLLocationDistance distance = [Location1 distanceFromLocation:Location2];
double distance1 = (distance/1000);
Satyanarayana
  • 1,059
  • 6
  • 16
0

First Of all You need to calculate the distance from Latitude and longitude.

To Calculate Distance Use the below Code:-

private double getDistance(Context context, Location location, Loation PerviousLocation) {
        if (location != null && PerviousLocation != null) {

            double lat = PerviousLocation.getLatitude();
            double longe = PerviousLocation.getLongitude(); ;

            float[] distance = new float[1];
            Location.distanceBetween(lat, longe, location.getLatitude(), location.getLongitude(), distance);
            return distance[0];
        }
        return 0;
    } 

Now find the time interval

  private double getDifferenceInTime(Context context, long currentTime, long previousTime) { 
    long diff = currentTime - previousTime;
    return diff / 1000;
}

Now Use The Speed Formula:-

Speed:- distance/time;

Some Time Location Object Also provide the speed. Use Can Use Location.getSpeed();

Umesh Saraswat
  • 560
  • 1
  • 8
  • 22