Location closestLocation;
int smallestDistance = -1;
假设位置是ArrayList()
for(Location location : locations){
int distance = Location.distanceBetween(closestLocation.getLatitude(),
closestLocation.getLongitude(),
location.getLatitude(),
location.getLongitude());
if(smallestDistance == -1 || distance < smallestDistance){
closestLocation = location;
smallestDistance = distance;
} }
但如何从latlng list
创建位置列表答案 0 :(得分:0)
您需要创建一个LatLngs
的arrayList。在forloop中,您将创建一个新的LatLng
对象并添加到arrayList。以下是代码的外观:
ArrayList<LatLng> latLngArrayList = new ArrayList<>();
for(Location location : locations){
//create new latlng object
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
int distance = Location.distanceBetween(closestLocation.getLatitude(),
closestLocation.getLongitude(),
location.getLatitude(),
location.getLongitude());
if(smallestDistance == -1 || distance < smallestDistance){
closestLocation = location;
smallestDistance = distance;
}
//add new latlng to arraylist
latLngArrayList.add(newLatLng);