【问题标题】:Finding the nearest location from a list of coordinates从坐标列表中查找最近的位置
【发布时间】:2014-06-19 20:16:15
【问题描述】:

我想从位置数组(lat、lng)中找到给定地址(用户输入)的最近位置。

(或者,更好的是,按距离对 lcoations 进行排序)

我想在不显示地图的情况下实现这一目标。 我也不确定哪个 Google API 最适合这个(地理编码 API、地图 v3 等)。

最好的方法是什么?

如果有人能指出我正确的方向,将不胜感激。

地点:

[
    {
        // Amsterdam
        "lat": 52.3702157,
        "lng": 4.8951679
    },
    {
        // Berlin
        "lat": 52.5200066,
        "lng": 13.404954
    },
    {
        // Brussels
        "lat": 50.85,
        "lng": 4.35
    },
    {
        // Paris
        "lat": 48.856614,
        "lng": 2.3522219
    },
    {
        // Madrid
        "lat": 40.4167754,
        "lng": -3.7037902
    }
]

用户输入: Antwerp

{
    "lat": 51.2194475,
    "lng": 4.4024643
}

应该返回: Brussels

{
    "lat": 50.85,
    "lng": 4.35
}

或者,按距离排序:

[
    {
        // Brussels
        "lat": 50.85,
        "lng": 4.35
    },
    {
        // Amsterdam
        "lat": 52.3702157,
        "lng": 4.8951679
    },
    {
        // Paris
        "lat": 48.856614,
        "lng": 2.3522219
    },
    {
        // Berlin
        "lat": 52.5200066,
        "lng": 13.404954
    },
    {
        // Madrid
        "lat": 40.4167754,
        "lng": -3.7037902
    }
]

【问题讨论】:

标签: google-maps-api-3 google-geocoding-api


【解决方案1】:

Android 为您提供了一个很好的功能,可以计算距离 (DistanceTo()) (ref: http://developer.android.com/reference/android/location/Location.html) 下面是一个例子,如何使用 DistanceTo 和 google map api:

    private double CalculateDistance(LatLng endPoint)
    {
        //Convert LatLng to Location
        Location location = new Location("tmploc");
        location.Latitude = endPoint.Latitude;
        location.Longitude = endPoint.Longitude;
        location.Time = currentLocation.Time; //Set time as current position's time. (could also be datetime.now)
        return currentLocation.DistanceTo(location);
    }

然后您可以使用它来找到最小值:

public int Lowest(params int[] inputs)
{
  return inputs.Min();
}

(参考:https://stackoverflow.com/a/4390808/3861983

如果您使用第一种方法执行方法,请将其添加到数组中,然后从第二种方法中获取最小的数字。你会实现你的目标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2017-05-29
    • 2012-03-09
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多