I have an android app that finds your current location but I need to figure out which zip codes are within, say, 20 miles of the current location. What would be the best way to find this information? Is there a public API or web service? I can give latitude/longitude or zip code as input.
You will have to use a reverse-geocoder, for example :
I would start by using the Haversine formula. Use the result you get from this to find the zipcode relative to that radius.
import com.google.android.maps.GeoPoint;
public class DistanceCalculator {
private double Radius;
// R = earth's radius (mean radius = 6,371km)
// Constructor
DistanceCalculator(double R) {
Radius = R;
}
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6()/1E6;
double lat2 = EndP.getLatitudeE6()/1E6;
double lon1 = StartP.getLongitudeE6()/1E6;
double lon2 = EndP.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
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.asin(Math.sqrt(a));
return Radius * c;
}
}
据我所知,目前还没有api,但是您可以使用邮政编码数据库http://zips.sourceforge.net/来创建自己的数据库以供使用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.