2011-01-24 87 views
6

我有一個「社交」地理意識應用程序,我正在開發,而百萬美元的問題是如何列出「我的位置」的「X英里內」的一組項目,因爲有那麼,我驚奇地發現,只有Google Maps API爲此提供免費的Web服務,更糟糕的是,只有在Google Map中使用Google服務時才支持。那麼我需要開發自己的距離計算器嗎?有沒有免費/付費服務可以讓我至少將地址轉換爲XY座標?如何找到兩個地址之間的距離? (Java服務器端)

我敢肯定有一個行業標準溶液(免費或商業),但我還沒有找到它

回答

4

實際上,Google確實可以在服務器端使用web services來實現此目的。

首先,您需要使用Geocoding API將地址轉換爲緯度/經度座標對。然後,你可以使用那些你認爲合適的(例如,如果你正在存儲這些數據庫)

如果你想找到的附近物品是Google可能已經知道的世界上的實際位置,你可以試試谷歌新的Places API它可以給你一定範圍內的結果。

您應該注意到從經緯度座標到距離的轉換確實需要一些數學計算,但我認爲最好在本地完成(在您的應用程序運行的服務器上),而不是遠離某些外部服務器這是只有數學。 谷歌搜索生產this

4

如果你使用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可以使用地理包執行此操作

4

如果你很幸運,並且你使用的是mysql或postgresql,它們都是啓用空間的數據庫,你可以對它們執行空間查詢。對於mysql,有spatial extensions,對於postgresql有postgis
如果你決定去手動,請看看這個question頭像up.Also我不明白如何谷歌地方的API可以幫助你,因爲你正在尋找你的數據半徑內,而不是谷歌的地方。 乾杯

3
  1. 你想要的「直線距離」(直線)距離,或行駛距離/時間?

  2. Mapquest的API似乎更容易,因爲您可以使用查詢字符串。

另一種選擇是geocoder.us(或.ca如果加拿大)

相關問題