如何找到两个地址之间的距离? (Java服务器端)

发布于 2024-10-13 06:12:54 字数 229 浏览 4 评论 0原文

我正在开发一个“社交”地理感知应用程序,价值百万美元的问题是如何列出“我的位置”“X 英里内”的一组项目,因为有数百万个应用程序可以做到这一点,我惊讶地发现只有 Google Maps API 具有免费的网络服务,更糟糕的是,只有在 Google Map 中使用时才支持它。那么我必须开发自己的距离计算器吗?有没有免费/付费服务可以让我至少将地址转换为 XY 坐标?

我确信有一个行业标准解决方案(免费或商业),但我还没有找到它

I have a "social" geographic aware application I'm developing, and the million dollar question is how do I list a set of items that are "within X miles" of "my location" since there are million applications that do that, I was surprised to find that only Google Maps API has a free web service for that, and worse, it is only supported if used within a Google Map. So do I have to develop my own distance calculator? is there any free / paid service that will allow me to at least transform an address to a XY coordinate?

I'm sure there is an industry standard solution (either free or commercial) but I'm yet to find it

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

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

发布评论

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

评论(4

[浮城] 2024-10-20 06:12:54

实际上,Google 确实有可以在服务器上使用的网络服务方来实现这一点。

首先,您需要使用地理编码 API 将地址转换为纬度/经度坐标对。然后,您可以使用您认为合适的方式(即,如果您要存储这些内容,则针对您自己的数据库)

如果您想要查找的附近项目是 Google 可能已经知道的世界上的实际位置,您可以尝试 Google 的新 Places API,它可以为您提供特定半径内的结果。

您应该注意,从纬度/经度坐标到距离的转换确实需要一些数学,但我认为最好在本地完成(在您的应用程序运行的服务器上),而不是外包给某些外部服务器,因为它是只有数学。
谷歌搜索产生了这个

Actually, Google does have web services you can use on the server side to achieve this.

First you'll want to use the Geocoding API to translate addresses into latitude/longitude coordinate pairs. You can then use those however you see fit (i.e. against your own database if you are storing those)

If the nearby items you want to find are actual places in the world that Google probably already knows about, you can try Google's new Places API which can give you results within a certain radius.

You should note that the translation from latitude/longitude coordinates to distances does require some math, but I think it is best done locally (on the server your application is running on) rather than being farmed out to some external server since it's only math.
Googling produced this.

空名 2024-10-20 06:12:54

如果你使用 SQL(你没有说)...我想我从 NerdDinner 项目复制了这个:

ALTER FUNCTION [dbo].[DistanceBetween] (@Lat1 as real,
                @Long1 as real, @Lat2 as real, @Long2 as real)
RETURNS real
AS
BEGIN

DECLARE @dLat1InRad as float(53);
SET @dLat1InRad = @Lat1 * (PI()/180.0);
DECLARE @dLong1InRad as float(53);
SET @dLong1InRad = @Long1 * (PI()/180.0);
DECLARE @dLat2InRad as float(53);
SET @dLat2InRad = @Lat2 * (PI()/180.0);
DECLARE @dLong2InRad as float(53);
SET @dLong2InRad = @Long2 * (PI()/180.0);

DECLARE @dLongitude as float(53);
SET @dLongitude = @dLong2InRad - @dLong1InRad;
DECLARE @dLatitude as float(53);
SET @dLatitude = @dLat2InRad - @dLat1InRad;
/* Intermediate result a. */
DECLARE @a as float(53);
SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad)
                 * COS (@dLat2InRad)
                 * SQUARE(SIN (@dLongitude / 2.0));
/* Intermediate result c (great circle distance in Radians). */
DECLARE @c as real;
SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a));
DECLARE @kEarthRadius as real;
/* SET kEarthRadius = 3956.0 miles */
SET @kEarthRadius = 6376.5;        /* kms */

DECLARE @dDistance as real;
SET @dDistance = @kEarthRadius * @c;
return (@dDistance);
END


ALTER FUNCTION [dbo].[NearestPeople]
    (
    @lat real,
    @long real,
    @maxdist real
    )
RETURNS  TABLE
AS
    RETURN
    SELECT     Person.ID
    FROM       Person
    WHERE dbo.DistanceBetween(@lat, @long, Latitude, Longitude) < @maxdist

然后我使用服务器中的这些 SQL 函数,就像 C# 中的这样:

public IQueryable<Person> FindNearbyPeople(float latitude, float longitude, float maxdistance)
{
    var people = from person in FindAllPeople()
                 join i in db.NearestPeople(latitude, longitude, maxdistance)
                 on person.ID equals i.ID
                 select person;

    return people;
}

这告诉我谁(在这种情况下,人们) )在最大距离内靠近我。

这是免费版本。我认为 SQL Server 2008 可以使用地理包来执行此操作

if you're using SQL (you didn't say)... I think I copied this from the NerdDinner project:

ALTER FUNCTION [dbo].[DistanceBetween] (@Lat1 as real,
                @Long1 as real, @Lat2 as real, @Long2 as real)
RETURNS real
AS
BEGIN

DECLARE @dLat1InRad as float(53);
SET @dLat1InRad = @Lat1 * (PI()/180.0);
DECLARE @dLong1InRad as float(53);
SET @dLong1InRad = @Long1 * (PI()/180.0);
DECLARE @dLat2InRad as float(53);
SET @dLat2InRad = @Lat2 * (PI()/180.0);
DECLARE @dLong2InRad as float(53);
SET @dLong2InRad = @Long2 * (PI()/180.0);

DECLARE @dLongitude as float(53);
SET @dLongitude = @dLong2InRad - @dLong1InRad;
DECLARE @dLatitude as float(53);
SET @dLatitude = @dLat2InRad - @dLat1InRad;
/* Intermediate result a. */
DECLARE @a as float(53);
SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad)
                 * COS (@dLat2InRad)
                 * SQUARE(SIN (@dLongitude / 2.0));
/* Intermediate result c (great circle distance in Radians). */
DECLARE @c as real;
SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a));
DECLARE @kEarthRadius as real;
/* SET kEarthRadius = 3956.0 miles */
SET @kEarthRadius = 6376.5;        /* kms */

DECLARE @dDistance as real;
SET @dDistance = @kEarthRadius * @c;
return (@dDistance);
END


ALTER FUNCTION [dbo].[NearestPeople]
    (
    @lat real,
    @long real,
    @maxdist real
    )
RETURNS  TABLE
AS
    RETURN
    SELECT     Person.ID
    FROM       Person
    WHERE dbo.DistanceBetween(@lat, @long, Latitude, Longitude) < @maxdist

I then use these SQL functions from the server like this in C#:

public IQueryable<Person> FindNearbyPeople(float latitude, float longitude, float maxdistance)
{
    var people = from person in FindAllPeople()
                 join i in db.NearestPeople(latitude, longitude, maxdistance)
                 on person.ID equals i.ID
                 select person;

    return people;
}

that tells me who (in this case, people) is close to me within a maximum distance.

this is the free version. I think SQL Server 2008 can perform this with a Geographic package

嘿看小鸭子会跑 2024-10-20 06:12:54

如果你幸运的话,你使用的是 mysql 或 postgresql,它们都是支持空间的数据库,你可以对它们执行空间查询。对于 mysql,有 空间扩展 对于 postgresql 有 postgis
如果您决定手动,请查看此 问题请注意。另外,我不明白 google place api 如何帮助您,因为您正在查找半径内的数据,而不是 google place 。
干杯

If you are lucky and you are using mysql or postgresql which both are spatial enabled databases you can execute spatial queries against them.For mysql there are spatial extensions and for postgresql there is postgis.
If you decide to go manual please take look at this question for heads up.Also i don't understand how the google places api can help you since you are looking your data within a radius and not google places.
Cheers

赏烟花じ飞满天 2024-10-20 06:12:54
  1. 您想要“As The Crow Flies”(直线)距离,还是行驶距离/时间?

  2. Mapquest 的 API 似乎更简单,因为您只需使用查询字符串即可。

另一种选择是 geocoder.us(如果是加拿大人,则为 .ca)

  1. Do you want the "As The Crow Flies" (straight line) distance, or driving distance/time?

  2. Mapquest's API seems to be easier as you can just use a query string.

Another option is geocoder.us (or .ca if Canadian)

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