如何在谷歌地图等地图路线上显示路况并估算时间?

时间:2018-10-05 07:36:55

标签: android

我正在做一个项目,我想在地图路线上显示道路交通和时间估算值,例如Googlemap。 。![Like this image ] 1

if (direction.isOK()) {
                Route route = direction.getRouteList().get(0);
                ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
                polyline = mGoogleMap.addPolyline(DirectionConverter.createPolyline(mActivity, directionPositionList, 5
                        , Color.BLUE));
                setCameraWithCoordinationBounds(route);

                    Leg leg = route.getLegList().get(0);
                    Info distanceInfo = leg.getDistance();
                    Info durationInfo = leg.getDuration();
                    String distance = distanceInfo.getText();
                    String duration = durationInfo.getText();
}

2 个答案:

答案 0 :(得分:1)

如果您使用的是Google Maps sdk,则可以在应用定位后启用路况详细信息。

           @Override
           public void onMyLocationChange(Location arg0) {
             mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("Here"));

             //load the traffic now
              googleMap.setTrafficEnabled(true);
           }
          });

答案 1 :(得分:0)

您也可以尝试以下代码。它会正确初始化地图,然后在检测到您的当前位置后设置路况数据。

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            mMap.setMyLocationEnabled(true);
            // Check if we were successful in obtaining the map.
            if (mMap != null) {


             mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

           @Override
           public void onMyLocationChange(Location arg0) {
            // TODO Auto-generated method stub

             mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));

             //load the traffic now
              googleMap.setTrafficEnabled(true);
           }
          });

            }
        }
    }