android - 如何在 Android 中使用折线显示轨道路线
问题描述
我正在开发一个公共汽车跟踪应用程序,我正在使用来自服务器的服务获取位置。现在,我想展示公共汽车的运动并绘制一条适当的折线。我实现了其中的一部分,但面临两个主要问题:
每次显示总线标记时,它都不会被删除。因此,公共汽车的旧足迹仍然存在。虽然我到达了目的地,但我看到了许多公共汽车图标。
我可以通过加入纬度和经度来绘制折线,但它有时会显示一条直线。
private void setmMap() {
progressDialog.show();
if (broadcastReceiver == null)
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Testing", "inside the setmap");
// show progress dialog
try {
double latitude = intent.getDoubleExtra("lat", 22.560214);
double longitude = intent.getDoubleExtra("longi", 22.560214);
Log.d("SetMap", intent.getExtras().getString("time"));
LatLng startLocation = new LatLng(latitude, longitude);
m.setPosition(startLocation);
points.add(startLocation);
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mMap.addPolyline(options); //add Polyline
mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
progressDialog.cancel();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
state;
Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
progressDialog.cancel();
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
}
谢谢,阿林丹。
解决方案
1)您没有显示添加标记的部分代码m
,可能代码运行多次;
2) 似乎公交车位置传感器的轮询周期相当长,并且不允许跟踪公交车转弯(公交车可以在已知位置之间转几圈)。因此,您需要在已知巴士位置之间插入路径,例如使用Google Directions API。
推荐阅读
- .htaccess - 将一个子目录重写为另一个子目录
- java - TimerTask如何做循环数据库查询?
- excel - 根据行值隐藏/取消隐藏列
- prometheus - 警报管理器未触发任何警报
- php - 使用 get_post_meta 将 unix 时间戳更改回 mysql 日期时间
- python - 使用 qml 中的元素显示的 python 中的数组
- apache-nifi - 在 Apache NiFi 中,如何使用时区以 ISO 格式打印当前日期
- json - 如何将 ndarray 响应从烧瓶服务器发送到 python 客户端?
- sql - 默认主键索引或用户创建索引,在Oracle中将优先执行
- c# - 迭代 IEnumerable 时删除一项