Android:罗盘+列表视图中的距离

Waz*_*_Be 10 java gps android listview distance

我猜你们都在地图上试过"Google地方信息".这是一个靠近你的POI列表.

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

使用距离和小箭头制作列表视图非常容易,但我无法理解每次用户移动手机时如何更新此列表和箭头.

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

替代文字

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

非常感谢任何信息.

Ret*_*ier 10

您尝试实现的目标有几种选择.就个人而言,我建议您实现自己的适配器(在这种情况下最有可能通过扩展SimpleCursorAdapter)并将更新封装到距离文本和罗盘标题轮换中.

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

在该方法中,您有两种选择.迭代组成表示数据集合的每个项目并修改罗盘图形和距离文本,或者只是将当前位置和标题记录为类中的变量,并调用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);
}
Run Code Online (Sandbox Code Playgroud)