Question

I'am trying to measure the distance between two coordinates, i've implemented the haversine function, just like this:

package Services;

public class Route {

private double latitude1;
private double longitude1; 
private double latitude2;
private double longitude2; 


public Route(double latitude1, double longitude1, double latitude2, double longitude2) {

    this.latitude1 = latitude1;
    this.longitude1 = longitude1;
    this.latitude2 = latitude2;
    this.longitude2 = longitude2;
}

public double measureDistance() {

     double earthRadius = 3958.75;
     double dLat = Math.toRadians(latitude2-latitude1);
     double dLng = Math.toRadians(longitude2-longitude1);
     double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(latitude1)) * Math.cos(Math.toRadians(latitude2)) *
                   Math.sin(dLng/2) * Math.sin(dLng/2);

     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
     double dist = earthRadius * c;

    return dist; 
}
}

The code works fine and ouputting something. But as I use the haversine formula, the code just measure the distance in skyline, not the roads if you know what I mean. Is there any possible way to measure distance like google maps does? Without using the google maps API, suddly google takes cash after 2500 API calls in a day or something..

Was it helpful?

Solution

Google Maps API pricing basically only affects web maps: http://code.google.com/apis/maps/faq.html#usage_apis

Google Maps for Android API is still free.

OTOH, there is no routing or navigation functionality in Google Maps for Android, so there is no way to calculate route distance with this API.

Also, you might find this handy: Geographical distance

OTHER TIPS

You should try the Bing Maps Android SDK. They don't have an explicit usage quota.

Bing Maps does have a usage quota, however this does not apply for mobile apps. Bing Maps is definatelty worth looking into. You can use Bing Maps with Android in one of two ways. The first is to use the Bing Maps Android SDK: http://bingmapsandroidsdk.codeplex.com/ The second is to use Bing Maps V7 AJAX control with PhoneGap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top