如何使用 Java 代码找到两个 ZipCode 之间的距离?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7426710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:55:50  来源:igfitidea点击:

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

javaweb-servicesjakarta-ee

提问by Amit

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

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

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 ?

如果不使用经度和经度计算距离是不可能的,那么我可以获得给定 ZIPCODE 的经度/经度值吗?(惊人)但是如何?

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.

任何教程/示例都会有很大帮助..因为我正在从事商业项目,所以不能使用Google Maps API。所以请不要推荐任何其他许可服务。

Thanks in advance,

提前致谢,

UPDATEone of the answers of this questionsuggests the use of MySQLor Postgress... Will that work for me..?

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

采纳答案by Amit

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...
  1. 首先,我将用户的邮政编码作为输入,然后使用免费提供的 API 之一找到纬度和经度值,
  2. 获得纬度、经度对后,我就可以轻松使用@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.

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

回答by Bohemian

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

如果您有兴趣,这是我对半正弦公式的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;
}

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

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

回答by Shahzeb

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

亲爱的艺术家,您有一种错觉,认为邮政编码可能是国家和州的本地,而不是地质分界。即使您的当地政府\议会\邮局有任何此类信息,您也只能在本地实施。除了抱歉,没有艺术家可以画这幅画。——

回答by Ali

There is a US Zip-codes Databasethat 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:

有一个美国的拉链代码数据库,你可以购买,为您提供经度和纬度每个邮编。我相信他们还可以让您访问他们的 api,因此您可以执行各种操作,例如计算 2 个邮政编码之间的距离,在本例中为 Worcester 和 St. Louis。但是,您可以通过让您的 Java 应用程序像这样调用 url 来从他们的页面上抓取数据来逃脱:

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

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

The traversing the DOM for the direct lineor 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 distanceas well.

直线行驶距离遍历 DOM 。只要您的网站不会变得高容量,您就应该很高兴。如果您的站点容量很大,则说服管理层为数据花费 40 美元。由于该网站使用谷歌来计算距离,因此您也应该能够获得步行距离

For your worldrequirement. 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 codeusing only the postal code and country name with Google Maps.

如果您使用 Google 地图,您甚至可以只使用国家/地区名称和邮政编码。使用 Google 地图仅使用邮政编码和国家/地区名称即可在英国邮政编码和法国邮政编码之间步行距离

回答by Pratik

check this post link and google geocoding and reverse geocoding

检查这个帖子链接和谷歌地理编码和反向地理编码

get latitude and longitude using zipcode

使用邮政编码获取纬度和经度

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

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