首页 > 解决方案 > 如何在 Android 中使用折线显示轨道路线

问题描述

我正在开发一个公共汽车跟踪应用程序,我正在使用来自服务器的服务获取位置。现在,我想展示公共汽车的运动并绘制一条适当的折线。我实现了其中的一部分,但面临两个主要问题:

  1. 每次显示总线标记时,它都不会被删除。因此,公共汽车的旧足迹仍然存在。虽然我到达了目的地,但我看到了许多公共汽车图标。

  2. 我可以通过加入纬度和经度来绘制折线,但它有时会显示一条直线。

为此,我附上了两张截图。 在此处输入图像描述

在此处输入图像描述 我使用的代码在这里:

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"));
}

谢谢,阿林丹。

标签: androidgoogle-mapsgoogle-polyline

解决方案


1)您没有显示添加标记的部分代码m,可能代码运行多次;

2) 似乎公交车位置传感器的轮询周期相当长,并且不允许跟踪公交车转弯(公交车可以在已知位置之间转几圈)。因此,您需要在已知巴士位置之间插入路径,例如使用Google Directions API


推荐阅读