如何在给定半径内找到最近的城市? [英] How to find nearest cities in a given radius?

查看:29
本文介绍了如何在给定半径内找到最近的城市?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否知道一些实用程序或网站,我可以在其中输入以英里为单位的美国城市、州和径向距离,然后它会返回该半径内的所有城市?

Do you know some utility or a web site where I can give US city,state and radial distance in miles as input and it would return me all the cities within that radius?

谢谢!

推荐答案

这是我的做法.

您可以获得城市、街道、邮政编码及其纬度和经度的列表.(我不记得我们从哪里得到我们的)

You can obtain a list of city, st, zip codes and their latitudes and longitudes. (I can't recall off the top of my head where we got ours)

http://geonames.usgs.gov/domestic/download_data.htm就像上面提到的人可能会工作.

edit: http://geonames.usgs.gov/domestic/download_data.htm like someone mentioned above would probably work.

然后您可以编写一个方法来根据半径计算最小和最大纬度和经度,并查询这些最小和最大之间的所有城市.然后循环并计算距离并删除任何不在半径内的东西

Then you can write a method to calculate the min and max latitude and longitudes based on a radius, and query for all cities between those min and max. Then loop through and calculate the distance and remove any that are not in the radius

double latitude1 = Double.parseDouble(zipCodes.getLatitude().toString());
double longitude1 = Double.parseDouble(zipCodes.getLongitude().toString());

//Upper reaches of possible boundaries
double upperLatBound = latitude1 + Double.parseDouble(distance)/40.0;
double lowerLatBound = latitude1 - Double.parseDouble(distance)/40.0;

double upperLongBound = longitude1 + Double.parseDouble(distance)/40.0;
double lowerLongBound = longitude1 - Double.parseDouble(distance)/40.0;

//pull back possible matches
SimpleCriteria zipCriteria = new SimpleCriteria();
zipCriteria.isBetween(ZipCodesPeer.LONGITUDE, lowerLongBound, upperLongBound);
zipCriteria.isBetween(ZipCodesPeer.LATITUDE, lowerLatBound, upperLatBound);
List zipList = ZipCodesPeer.doSelect(zipCriteria);
ArrayList acceptList = new ArrayList();

if(zipList != null)
{
    for(int i = 0; i < zipList.size(); i++)
    {
        ZipCodes tempZip = (ZipCodes)zipList.get(i);
        double tempLat = new Double(tempZip.getLatitude().toString()).doubleValue();
        double tempLon = new Double(tempZip.getLongitude().toString()).doubleValue();
        double d = 3963.0 * Math.acos(Math.sin(latitude1 * Math.PI/180) * Math.sin(tempLat * Math.PI/180) + Math.cos(latitude1 * Math.PI/180) * Math.cos(tempLat * Math.PI/180) *  Math.cos(tempLon*Math.PI/180 -longitude1 * Math.PI/180));

        if(d < Double.parseDouble(distance))
        {
            acceptList.add(((ZipCodes)zipList.get(i)).getZipCd());  
        }
    }
}

这是我的代码的摘录,希望你能看到发生了什么.我从一个邮政编码(我数据库中的一个表格)开始,然后我撤回可能的匹配项,最后我淘汰了不在半径范围内的那些.

There's an excerpt of my code, hopefully you can see what's happening. I start out with one ZipCodes( a table in my DB), then I pull back possible matches, and finally I weed out those who are not in the radius.

这篇关于如何在给定半径内找到最近的城市?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆