How can I include the traffic layer of google maps?

。_饼干妹妹 提交于 2019-11-30 08:22:05

To be able to show traffic data you should consider the following issues,

  1. Make sure your current location is detected in Google Map

  2. Make sure your Google Map has traffic data available for your current location.

You may also try the following code. It initializes the map properly then sets traffic data after detecting your current location.

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

                }
            }
        }

Try the following code in your activity in which you want to load the map:

private GoogleMap googleMap;
protected LocationManager locationManager;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


    try {
                // Loading map
                initilizeMap();

                // Changing map type
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                // googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);

                // Showing / hiding your current location
                googleMap.setMyLocationEnabled(true);
                googleMap.setTrafficEnabled(true);
                // Enable / Disable zooming controls
                googleMap.getUiSettings().setZoomControlsEnabled(true);

                // Enable / Disable my location button
                googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                // Enable / Disable Compass icon
                googleMap.getUiSettings().setCompassEnabled(true);

                // Enable / Disable Rotate gesture
                googleMap.getUiSettings().setRotateGesturesEnabled(true);

                // Enable / Disable zooming functionality
                googleMap.getUiSettings().setZoomGesturesEnabled(true);
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


            } catch (Exception e) {
                e.printStackTrace();

            }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!