【问题标题】:Android: compass + distance in a listviewAndroid:列表视图中的指南针+距离
【发布时间】:2013-06-24 16:38:08
【问题描述】:

我猜你们都尝试过在地图中使用“Google 地方信息”。 这是您附近的 POI 列表。

我真的很想在我的应用程序中使用 GPS 坐标列表来执行相同的功能,但这看起来真的很复杂。

用距离和小箭头制作列表视图非常容易,但我不明白每次用户移动手机时如何更新此列表和箭头。

现在我有一个静态列表视图。

我想知道是否有人成功创建了这样的列表视图。

非常感谢您提供任何信息。

【问题讨论】:

    标签: java android listview gps distance


    【解决方案1】:

    您想要实现的目标有几个选项。就个人而言,我建议您实现自己的适配器(在这种情况下,最有可能通过扩展 SimpleCursorAdapter)并将距离文本和罗盘方向旋转的更新封装在其中。

    为了帮助进行资源管理,您可能希望在托管 ListView 的 Activity 中创建 SensorListener 和 LocationListener。每当您收到更新时,请从您的 Adapter 类中调用您的自滚动 updateCompassAndLocation 方法。

    在该方法中,您有两个选择。要么遍历组成被表示的数据集合的每个项目并修改罗盘图形和距离文本,要么简单地将当前位置和航向记录为类中的变量,并调用notifyDataSetChanged 强制适配器更新在getView 方法中查看自身。无论哪种情况(尤其是后者),您都需要在 getView 中设置距离文本和航向罗盘值。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      LinearLayout myView;
    
      MyPOI item = getItem(position);
    
      Location poiLocation = item.getLocation;
    
      int compassHeading = // Calculate heading relative to current heading
      float distance = // Calculate distance to POI
    
    
      if (convertView == null) {
        myView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, myView, true);
      } else {
        trainView = (LinearLayout) convertView;
      }
    
      TextView distanceView = (TextView)trainView.findViewById(R.id.distance);
      ImageView compassView = (ImageView)trainView.findViewById(R.id.compass);
    
      distanceView.setText(String.valueOf(distance);
      compassView.setImageLevel(compassHeading);
    }
    

    【讨论】:

    • 抱歉耽搁了。几个月前我尝试了您的解决方案,对此我感到非常满意。非常感谢!
    • @Waza_Be 如果您可以发布您为此实施的解决方案,那就太好了,我也在尝试做同样的事情,我正面临指南针航向旋转的问题。谢谢..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多