Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the distance between two ZipCodes using Java Code?

My requirements are similar to this question, except the fact that I am prohibited to use the latitude and longitude values. I want to caluclate the "walking" distance between two zipcodes. Later I would also require to query "who is within X kms ?"

I am not sure whether it is achievable or not. Is it really possible to find the distance between two given zipcodes ? I don't want the answers which will work only for US/UK zip codes. I need a generic solution which will work for any two zipcodes of the world.

If the calculation of distance without using Langitude & longitude is not possible then can I get the Langitude / longitude values of a given ZIPCODE ? (amazing) but How ?

Any tutorial / example will be of great help..Since I am working on a commercial project cant use the Google Maps API. So please don't suggest any other licensed services.

Thanks in advance,

UPDATE one of the answers of this question suggests the use of MySQL or Postgress... Will that work for me..?

like image 654
Amit Avatar asked Sep 15 '11 06:09

Amit


People also ask

How do I find the distance between two zip codes?

The Latitude and Longitude are needed to calculate the distance between two locations with following formula: =acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon2-lon1))*6371 (Ps: 6371 is Earth radius in km.)

How do you calculate Euclidean distance in Java?

* Euclidean distance: distance = square root of (x squared + y squared). * Distance from (0, 0) to (3, 4) is 5.0.

How do you find the distance between two points using latitude and longitude in Java?

I found the following formula: function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) { var R = 6371; // Radius of the earth in km var dLat = deg2rad(lat2-lat1); // deg2rad below var dLon = deg2rad(lon2-lon1); var a = Math. sin(dLat/2) * Math.


2 Answers

If you're interested, here's my java implementation of the haversine formula

/**
 * Calculates the distance in km between two lat/long points
 * using the haversine formula
 */
public static double haversine(
        double lat1, double lng1, double lat2, double lng2) {
    int r = 6371; // average radius of the earth in km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
       Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) 
      * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return d;
}

I hereby donate this to the public arena under GPL :)

like image 113
Bohemian Avatar answered Oct 13 '22 18:10

Bohemian


Just copy and paste of my comment ;)


Dear artist you are under an illusion that it is possible postcodes are local to Countries and states and are not geological demarcations . You can only implement this locally even that if your local government\ council\postoffice has any such information. Other than sorry to say no Artist can paint this picture. –

like image 30
Shahzeb Avatar answered Oct 13 '22 16:10

Shahzeb