【问题标题】:How to add locations and distances to a custom layout android如何将位置和距离添加到自定义布局android
【发布时间】:2013-06-02 22:31:15
【问题描述】:

我正在制作一个 android 应用程序,该应用程序将列出不在 google 位置上的特定位置。我有所有的经纬度和地名,它们不会改变。我可以将它们显示在我的自定义列表中,它工作正常问题是我想按与您(用户)位置的距离对它们进行排序并显示它们旁边的距离。

我尝试了很多不同的方法,但有点卡住了。我想说我是编程新手,并且在这个应用程序中遇到了挫折,如果有人能提供帮助,我将不胜感激。

所以我要问的问题是我如何/应该如何按距离对位置进行排序,以便将它们添加到我的自定义列表中。

// create array to hold place names to be looped through later
    String[] placenames = { "place1", "place2",
            "place3", "place4" };

// // create arrays to hold all the latitudes and longitudes
    double[] latArray = new double[] { 51.39649, 51.659775, 51.585433,
            51.659775 };
    double[] lngArray = new double[] { 0.836523, 0.539901, 0.555385,
            0.539901, };

// hard code my location for test purposes only
    Location MyLocation = new Location("My location");
    MyLocation.setLatitude(51.659775);
    MyLocation.setLongitude(0.539901);

    for (int i = 0; i < placenames.length;) {

// Place location object
        Location PlaceName = new Location(placenames[i]);
        PlaceName.setLatitude(latArray[i]);
        PlaceName.setLongitude(lngArray[i]);
        i++;

// calculate distance in meters
        float distanceInMeters = PlaceName.distanceTo(MyLocation);

// convert to double

        double DistanceInMiles = distanceInMeters * 0.000621371;

        dimint = (int) DistanceInMiles;

// format numbers to two decimal places
        DecimalFormat df = new DecimalFormat("#.##");
        dim = df.format(DistanceInMiles);

//make treemap and then sortedmap to sort places by distance

        TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

        SortedMap<Integer, String> treemapsorted = new TreeMap<Integer,String>();

        treemap.put(dimint, PlaceName.getProvider());

        treemapsorted = treemap.subMap(0, 5);

// Toast for test purpose to see if sort is working

        Toast tst = Toast.makeText(this, treemapsorted.entrySet()
                .toString(), Toast.LENGTH_SHORT);
        tst.show();

        CustomList place_data[] = new CustomList[] {

// This is the problem part 

        new CustomList(R.drawable.picture1, treemapsorted.get(dimint)),

        };

        CustomListAdapter adapter = new CustomListAdapter(this,
                R.layout.listview_item_row, place_data);
        listView1 = (ListView) findViewById(R.id.listView1);
        listView1.setAdapter(adapter);
        ;
    }

【问题讨论】:

    标签: android list sorting location


    【解决方案1】:

    我刚刚查看了您的代码,发现了很多问题:

    1. 在 for 循环语句中使用 i++,而不是在循环体中,如下所示:

      for (int i = 0; i < placenames.length; i++) {
      
      }
      
    2. 您以英里为单位计算距离,将其转换为 int(我将在此处使用 Math.round()),然后使用您不使用的 DecimalFormat 创建一个字符串。

    3. 您在每次循环迭代中创建一个 TreeMap。将创建移动到循环前面并在循环体中添加项目。

    4. TreeMap 不是此任务的正确类。我测试了您的代码,循环后 Map 仅包含 3 个项目。原因是 TreeMap 包含键值对,其中键必须是唯一的。添加一个带有键(在您的情况下为距离)的元素,该元素已经在地图中,会导致覆盖该元素。因此,我建议不要使用TreeMap,而是使用ArrayList。您需要使用所需的所有变量创建一个类,例如距离和地名。该类需要实现接口Comparable&lt;Class&gt;。然后,您必须实现方法public int compareTo(T other),在该方法中您将距离与另一个对象的距离进行比较。然后您可以使用Collections.sort(arrayList) 对 ArrayList 进行排序。在 for 循环主体中,您应该将项目添加到该 ArrayList,然后对其进行排序,然后遍历 ArrayList 项目并将它们添加到您的 ListView

    【讨论】:

    • 感谢您的帮助。我不知道为什么我将 i++ 放在循环之外,但感谢您发现它。正如您建议的那样,我使用了一个 ArrayList,该应用程序现在正在运行。再次感谢您的快速回复。
    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多