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

5

10

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..?

Cynthea answered 15/9, 2011 at 6:26 Comment(7)
You said that you don't want to work with Lat/Lon values, but, it would be way easier if you Google those values beforehand, hardcode it in or something, and then make the math with that. (It's simple math to get distance from Lat/Lon)Prospero
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.Inopportune
@Nideo your absolutely right. And it's not compulsory that you got the zipcode are available for every city, town, etc. may be you didn't get the exact location by zip codeHarkness
thanks @Shahzeb.... thats what I wanted to know "the feasibility of the thought".. I have some basic knowledge of the "distance calculation using latitude and longitude values" but I am instructed not to use lat / lon values... thanks for your quick help..Cynthea
pasted it as answer might be cheeky but feel to accept no pressure.Inopportune
He is asking about zip codes yet everybody is blatantly posting long lat solutions.Claudetta
You can use thezipcodes.com to get location from ZIP Code and calculate distance. To integrate with your service follow thezipcodes.com/docsSchmid
C
4

I followed a two step process...

  1. First, I took the user's zipcode as input, than found the latitude and longitude values by using one of the freely available APIs,
  2. Once I got the latitude, longitude pair,I was able to easily use the function suggested by @Bohemian...

However after sometimes I found that google and Yahoo APIs are the best possible way for this IMHO, so I integrated that in my project. If any one interested I can paste the code for that here.

Cynthea answered 15/9, 2011 at 9:55 Comment(0)
A
14

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 :)

Attributive answered 15/9, 2011 at 7:32 Comment(1)
+1from me too due to importance of this code but not what OP wants mate :)Claudetta
I
5

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. –

Inopportune answered 15/9, 2011 at 6:59 Comment(1)
This was rather a bad idea to do it myself.. The best way is to use third party API as I mentioned in this question https://mcmap.net/q/1019968/-geocode-area-names-of-a-city-to-get-lat-and-longCynthea
V
4

There is a US Zip-codes Database that you can purchase that provides you the Longitude and Latitude for each zip-code. I believe they also give you access to their api so you can do all sorts of stuff like calculate the distance between 2 zipcodes, in this case Worcester and St. Louis. However you may be able to get away by scraping data off their page by just getting your java application to call a url like so:

http://www.zip-codes.com/distance_calculator.asp?zip1=63130&zip2=01610

The traversing the DOM for the direct line or driving distance. As long as your website doesn't become high volume you should be good to go. If your site is high volume then convince the management to spent the $40 for the data. Since the website uses google to calculate the distance you should be able to get walking distance as well.

For your world requirement. The whole world sadly does not use zip-codes, some of us use what are called Postal Codes and I wouldn't be surprised if there is a third name of there or even a fourth for the same. It probably is possible to use the same solution for all, i.e. get logitude and lattitude and have Google maps tell you the walking distance. All you need is a database that maps the zip / postal code to the longitude and latitudes and have google maps map out the walking distance.

If you are leveraging Google maps, you may even be able to get away with just the country name and zip code. Walking distance between UK postal code and france postal code using only the postal code and country name with Google Maps.

Vistula answered 15/9, 2011 at 6:56 Comment(1)
@Ali... thanks but I have already mentioned that i don't want US/UK specific answers....Cynthea
C
4

I followed a two step process...

  1. First, I took the user's zipcode as input, than found the latitude and longitude values by using one of the freely available APIs,
  2. Once I got the latitude, longitude pair,I was able to easily use the function suggested by @Bohemian...

However after sometimes I found that google and Yahoo APIs are the best possible way for this IMHO, so I integrated that in my project. If any one interested I can paste the code for that here.

Cynthea answered 15/9, 2011 at 9:55 Comment(0)
H
2

check this post link and google geocoding and reverse geocoding

get latitude and longitude using zipcode

http://code.google.com/apis/maps/documentation/geocoding/

Harkness answered 15/9, 2011 at 6:33 Comment(1)
basically, if you google-map "zipcode, country" or "zipcode, city/town, country" you should be able to get the lat/lon for this area.Nicolenicolea

© 2022 - 2024 — McMap. All rights reserved.