To calculate the distance between two zipcode, you need a table which stores latitude and longitude for all the cities.
CREATE TABLE `zip_codes` (
`zip` varchar(5) NOT NULL default '',
`state` char(2) NOT NULL default '',
`latitude` varchar(10) NOT NULL default '',
`longitude` varchar(10) NOT NULL default '',
`city` varchar(50) default NULL,
`full_state` varchar(50) default NULL,
UNIQUE KEY `zip` (`zip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `zip_codes` VALUES ('35004', 'AL', ' 33.606379', ' -86.50249', 'Moody', 'Alabama');
`zip` varchar(5) NOT NULL default '',
`state` char(2) NOT NULL default '',
`latitude` varchar(10) NOT NULL default '',
`longitude` varchar(10) NOT NULL default '',
`city` varchar(50) default NULL,
`full_state` varchar(50) default NULL,
UNIQUE KEY `zip` (`zip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `zip_codes` VALUES ('35004', 'AL', ' 33.606379', ' -86.50249', 'Moody', 'Alabama');
You can use the below logic to calculate the distance:-
import java.lang.Math;
import java.lang.Double;
public int calcDistance(double latA, double longA, double latB, double longB)
{
double theDistance = (Math.sin(Math.toRadians(latA)) *
Math.sin(Math.toRadians(latB)) +
Math.cos(Math.toRadians(latA)) *
Math.cos(Math.toRadians(latB)) *
Math.cos(Math.toRadians(longA - longB)));
return = (Math.toDegrees(Math.acos(theDistance))) * 69.09;
}
import java.lang.Double;
public int calcDistance(double latA, double longA, double latB, double longB)
{
double theDistance = (Math.sin(Math.toRadians(latA)) *
Math.sin(Math.toRadians(latB)) +
Math.cos(Math.toRadians(latA)) *
Math.cos(Math.toRadians(latB)) *
Math.cos(Math.toRadians(longA - longB)));
return = (Math.toDegrees(Math.acos(theDistance))) * 69.09;
}