【问题标题】:How to find distance between two addresses? (Java server side)如何找到两个地址之间的距离? (Java服务器端)
【发布时间】:2011-01-24 05:41:24
【问题描述】:

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

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

【问题讨论】:

    标签: google-maps geolocation distance street-address


    【解决方案1】:

    实际上,Google 确实有 web services 你可以在服务器端使用来实现这一点。

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

    如果您要查找的附近项目是 Google 可能已经知道的世界上的实际地点,您可以尝试 Google 的新 Places API,它可以为您提供一定范围内的结果。

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

    【讨论】:

      【解决方案2】:

      如果您使用的是 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
      

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

      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 可以使用 Geographic 包执行此操作

      【讨论】:

        【解决方案3】:

        如果你很幸运,并且你使用的是 mysql 或 postgresql,它们都是启用空间的数据库,你可以对它们执行空间查询。对于 mysql,有 spatial extensions,对于 postgresql,有 postgis
        如果您决定手动操作,请查看此question 以获取提示。另外我不明白 google 地方 api 如何为您提供帮助,因为您正在查看您的数据在半径范围内而不是谷歌地方。 干杯

        【讨论】:

          【解决方案4】:
          1. 您想要“As The Crow Flies”(直线)距离,还是行驶距离/时间?

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

          另一个选项是 geocoder.us(如果是加拿大,则为 .ca)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-15
            • 1970-01-01
            相关资源
            最近更新 更多