如何从android中的Latlang数组列表创建位置对象列表?

时间:2018-02-08 13:08:57

标签: android google-maps location-services

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

创建位置列表

1 个答案:

答案 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);