Android: compass + distance in a listview(Android:列表视图中的指南针+距离)
问题描述
我猜你们都尝试过在地图中使用Google 地方信息".这是您附近的 POI 列表.
I guess you have all tried "Google Places" in Maps. This is a list of POI close to you.
我真的很想在我的应用程序中使用 GPS 坐标列表来执行相同的功能,但这看起来真的很复杂.
I really would like to do the same feature in my application with a list of GPS coordinates, but that seem really complicated.
用距离和小箭头制作列表视图很容易,但我不明白每次用户移动手机时如何更新这个列表和箭头.
Making the listview with the distance and the little arrow is very easy, but I cannot understand how to update this list and the arrow everytime the user move his phone.
现在我有一个静态列表视图.
For now I have a static listview.
我想知道是否有人成功创建了这样的列表视图.
I would like to know if someone succeed to create such a listview.
非常感谢您提供任何信息.
Thank a lot for any information.
推荐答案
您想要实现的目标有几个选项.就个人而言,我建议您实现自己的适配器(在这种情况下,最有可能通过扩展 SimpleCursorAdapter)并将距离文本和罗盘航向旋转的更新封装在其中.
There are a few options for what you're trying to achieve. Personally, I'd recommend implementing your own Adapter (most likely in this case by extending SimpleCursorAdapter) and encapsulating the updates to distance text and compass heading rotation within that.
为了帮助进行资源管理,您可能希望在托管 ListView 的 Activity 中创建 SensorListener 和 LocationListener.每当您收到更新时,从您的 Adapter 类中调用您的自滚动 updateCompassAndLocation
方法.
To help with resource management, you probably want to create a SensorListener and LocationListener within the Activity that will host the ListView. Whenever you receive an update call your self-rolled updateCompassAndLocation
method from within your Adapter class.
在该方法中,您有两个选择.要么遍历组成被表示的数据集合的每个项目并修改罗盘图形和距离文本,要么简单地将当前位置和航向记录为类中的变量,并调用 notifyDataSetChanged
强制适配器在 getView
方法中更新视图本身.无论哪种情况(尤其是后者),您都需要在 getView 中设置距离文本和航向罗盘值.
From within that method you have two options. Either iterate over each of the items that make up the data collection being represented and modify the compass graphic and distance text, or simply record the current location and heading as variables within the class, and call notifyDataSetChanged
to force the adapter to update the views itself within the getView
method. In either case (especially the latter), you'll need to set the distance text and heading compass values within 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);
}
这篇关于Android:列表视图中的指南针+距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:列表视图中的指南针+距离
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01