如何使用 Java 代码查找两个邮政编码之间的距离?

发布于 2024-12-04 19:28:09 字数 1016 浏览 0 评论 0 原文

我的要求类似于这个问题,除了我被禁止使用纬度和经度值这一事实之外。我想计算两个邮政编码之间的“步行”距离。后来我还需要查询“谁在X公里内?”

我不确定它是否可以实现。真的有可能找到两个给定邮政编码之间的距离吗? 我不想要仅适用于美国/英国邮政编码的答案。我需要一个适用于世界上任何两个邮政编码的通用解决方案。

如果计算距离时不使用Langitude &经度不可能,那么我可以获得给定邮政编码的经度/经度值吗? (惊人)但是如何?

任何教程/示例都会有很大帮助。由于我正在开发一个商业项目,因此无法使用 Google 地图 API。因此,请不要推荐任何其他许可服务。

预先感谢,

更新 这个问题的答案之一表明使用 MySQLPostgress...这对我有用吗..?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绝不服输 2024-12-11 19:28:09

如果您感兴趣,这是我对 haversine 公式 的 java 实现,

/**
 * 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;
}

我特此将其捐赠给 GPL 下的公共领域:)

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

笑饮青盏花 2024-12-11 19:28:09

只需复制并粘贴我的评论即可;)


亲爱的艺术家,您有一种错觉,认为邮政编码是可能的
是国家和州当地的,不是地质分界
。您只能在本地实施此操作,即使您的本地
政府\议会\邮局有任何此类信息。以外
遗憾的是,没有艺术家可以画出这幅画。 –

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

没有心的人 2024-12-11 19:28:09

您可以购买美国邮政编码数据库,它为您提供每个邮政编码的经度和纬度。我相信他们还允许您访问他们的 api,这样您就可以执行各种操作,例如 计算 2 个邮政编码之间的距离,在本例中为伍斯特和圣路易斯。不过,您也许可以通过让您的 Java 应用程序调用如下 url 来从其页面上抓取数据:

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

用于直线行驶的遍历 DOM距离。只要您的网站访问量不高,您就可以继续使用。如果您的网站流量很大,请说服管理层花费 40 美元购买数据。由于该网站使用谷歌来计算距离,因此您也应该能够获得步行距离

满足您的世界要求。遗憾的是,全世界都不使用邮政编码,我们中的一些人使用所谓的邮政编码,如果有第三个甚至第四个相同的名称,我不会感到惊讶。也许可以对所有人使用相同的解决方案,即获取经度和纬度,并让谷歌地图告诉您步行距离。您所需要的只是一个将邮政编码映射到经度和纬度的数据库,并让谷歌地图绘制出步行距离。

如果您使用谷歌地图,您甚至可以只使用国家/地区名称和邮政编码。 英国邮政编码 和法国邮政编码仅在 Google 地图上使用邮政编码和国家/地区名称。

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.

心安伴我暖 2024-12-11 19:28:09

我遵循了两步过程...

  1. 首先,我将用户的邮政编码作为输入,然后使用免费提供的 API 之一找到纬度和经度值,
  2. 一旦获得纬度、经度对,我就可以轻松使用@Bohemian 建议的功能...

但是有时我发现谷歌和雅虎 API 是这个恕我直言的最佳方法,所以我将其集成到我的项目中。如果有人感兴趣,我可以在这里粘贴代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文