我正在开发一个“社交”地理位置感知应用程序,关键问题是如何列出一组与“我的位置”相距“X英里”的项目,由于有数百万个应用程序可以做到这一点,我惊讶地发现只有 Google Maps API 提供免费的网络服务,更糟糕的是,只有在 Google 地图内使用时才支持。所以,我是否需要开发自己的距离计算器?是否有任何免费/付费服务,至少可以将地址转换为 XY 坐标?
我相信有行业标准解决方案(无论是免费还是商业),但我还没有找到。
我相信有行业标准解决方案(无论是免费还是商业),但我还没有找到。
如果您正在使用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
代码示例:
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;
}
这段文本是关于IT技术的,需要翻译成中文。它讲述了如何在最大距离范围内查找与自己接近的人。
这是免费版本。我认为SQL Server 2008可以通过地理包来实现此功能。
您需要“直线距离”还是“驾车距离/时间”?
Mapquest的API似乎更容易使用,因为您可以只使用查询字符串。
另一个选择是geocoder.us(如果在加拿大则为.ca)