我想从一系列地点(lat,lng)中为给定的地址(用户输入)找到最近的位置。
(或者,更好的是,按距离排序)
我想在不显示地图的情况下实现这一点。我也不确定哪个Google最适合这一点(Geocoding,Maps 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
}
]
发布于 2014-07-21 11:26:24
Android为您提供了一个很好的特性,它可以计算距离( DistanceTo ()) (ref:http://developer.android.com/reference/android/location/Location.html) Heres --一个如何在google中使用DistanceTo的示例:
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)
如果执行使用第一个方法的方法,则将其添加到数组中,然后从第二个方法中获得最低的数值。你就会实现你的目标。
https://stackoverflow.com/questions/24315538
复制