Page Summary
-
Follow these steps to integrate a map for trip following in your Android app: define a map fragment, add support for a base layer and view controller, and optionally enable vector graphics support.
-
Define your map using either
ConsumerMapFragment(for fragments) orConsumerMapView(for views) to display trip progress. -
Obtain the
ConsumerGoogleMapinstance throughgetConsumerGoogleMapAsync()and utilize it to interact with the map and access theConsumerControllerfor trip management features. -
For vector graphics support, ensure your Activity extends
AppCompatActivityto utilize Vector drawables within the Consumer SDK.
To follow a trip in your consumer app, you first need to define a map and add support for vector maps, if needed.
To set up a map in your app, follow these steps:
- Define a map fragment to follow a trip .
- Add support for a maps base layer and view controller .
- Add support for Android vector graphics to display vector maps, if needed.
After you've defined a map, you can add additional views and camera controls you want to customize the visual experience. For more details, see Style a map .
Step 1: Define a map fragment to follow a trip
You define a map by adding a map fragment or view to create the map where you share an on-demand trip in your consumer app. To define your map, follow one of these methods:
-
ConsumerMapFragment: Use to define your map with aFragment. -
ConsumerMapView: Use to define a map with aView.
The features are the same for either method, so choose which method is better for your application.
Both methods are explained in more detail in the following section.
Add a map fragment or view
To create a map to display trip progress using either an Android fragment or a view, follow these steps and refer to the code examples.
-
Define a fragment or view in your application layout XML file located in
/res/layout. Define the trip map as either a fragment usingConsumerMapFragment, or as a view usingConsumerMapView.The fragment or view then provides access to the trip map that your app can access and modify. The map also provides a handle to the
ConsumerController, which allows your app to control and customize the consumer experience. -
From your
onCreate()method, callgetConsumerGoogleMapAsync(callback), which returns theConsumerGoogleMapasynchronously in the callback. -
Use the
ConsumerGoogleMapto display trip progress and update as needed.
Example of how to add ConsumerMapFragment
-
Define the fragment in your application layout XML file, as shown in the following code example.
<fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.google.android.libraries.mapsplatform.transportation.consumer.view.ConsumerMapFragment" android:id="@+id/consumer_map_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> -
Make a call to
getConsumerGoogleMapAsync()from theonCreate()method.
Java
public
class
SampleAppActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
// Find the ConsumerMapFragment.
ConsumerMapFragment
consumerMapFragment
=
(
ConsumerMapFragment
)
fragmentManager
.
findFragmentById
(
R
.
id
.
consumer_map_fragment
);
// Initiate the callback that returns the map.
if
(
consumerMapFragment
!=
null
)
{
consumerMapFragment
.
getConsumerGoogleMapAsync
(
new
ConsumerMapReadyCallback
()
{
// The map returned in the callback is used to access the ConsumerController.
@Override
public
void
onConsumerMapReady
(
@NonNull
ConsumerGoogleMap
consumerGoogleMap
)
{
ConsumerController
consumerController
=
consumerGoogleMap
.
getConsumerController
();
}
});
}
}
}
Kotlin
class
SampleAppActivity
:
AppCompatActivity
()
{
override
fun
onCreate
(
savedInstanceState
:
Bundle?)
{
// Find the ConsumerMapFragment.
val
consumerMapFragment
=
fragmentManager
.
findFragmentById
(
R
.
id
.
consumer_map_fragment
)
as
ConsumerMapFragment
consumerMapFragment
.
getConsumerGoogleMapAsync
(
object
:
ConsumerMapReadyCallback
()
{
override
fun
onConsumerMapReady
(
consumerGoogleMap
:
ConsumerGoogleMap
)
{
val
consumerController
=
consumerGoogleMap
.
getConsumerController
()
!!
}
}
)
}
}
Example of how to add ConsumerMapView
-
Use the view either in a fragment or in an activity, as defined in your XML file.
<com.google.android.libraries.mapsplatform.transportation.consumer.view.ConsumerMapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/consumer_map_view" android:layout_width="match_parent" android:layout_height="match_parent" /> -
Make the call to
getConsumerGoogleMapAsync()fromonCreate(). In addition to the callback parameter, include the following details:-
The containing activity or fragment. The activity or fragment base class must be either a
FragmentActivityor a supportFragment(respectively), since they provide access to its lifecycle. -
GoogleMapOptions(which can be null), containing configuration attributes for theMapView.
-
Java
public
class
SampleAppActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
ConsumerMapView
mapView
=
findViewById
(
R
.
id
.
consumer_map_view
);
if
(
mapView
!=
null
)
{
mapView
.
getConsumerGoogleMapAsync
(
new
ConsumerMapReadyCallback
()
{
// The map returned in the callback is used to access the ConsumerController.
@Override
public
void
onConsumerMapReady
(
@NonNull
ConsumerGoogleMap
consumerGoogleMap
)
{
ConsumerController
consumerController
=
consumerGoogleMap
.
getConsumerController
();
}
},
this
,
null
);
}
}
}
Kotlin
class
SampleAppActivity
:
AppCompatActivity
()
{
override
fun
onCreate
(
savedInstanceState
:
Bundle?)
{
val
mapView
=
findViewById
(
R
.
id
.
consumer_map_view
)
as
ConsumerMapView
mapView
.
getConsumerGoogleMapAsync
(
object
:
ConsumerMapReadyCallback
()
{
// The map returned in the callback is used to access the ConsumerController.
override
fun
onConsumerMapReady
(
consumerGoogleMap
:
ConsumerGoogleMap
)
{
val
consumerController
=
consumerGoogleMap
.
getConsumerController
()
!!
}
},
/* fragmentActivity= */
this
,
/* googleMapOptions= */
null
,
)
}
}
A MapView
in a fragment is the same as for the preceding example for MapView
in an activity, except that the fragment inflates the layout that
includes the MapView
in the fragment onCreateView()
method.
Java
public
class
MapViewInFragment
extends
Fragment
{
@Override
public
View
onCreateView
(
@NonNull
LayoutInflater
layoutInflater
,
@Nullable
ViewGroup
viewGroup
,
@Nullable
Bundle
bundle
)
{
return
layoutInflater
.
inflate
(
R
.
layout
.
consumer_map_view
,
viewGroup
,
false
);
}
}
Kotlin
class
MapViewInFragment
:
Fragment
()
{
override
fun
onCreateView
(
layoutInflater
:
LayoutInflater
,
container
:
ViewGroup?,
savedInstanceState
:
Bundle?,
):
View
{
return
layoutInflater
.
inflate
(
R
.
layout
.
consumer_map_view
,
viewGroup
,
false
)
}
}
Step 2: Add support for a maps base layer and view controller
To share trip progress in your app, add the
following classes to your app: ConsumerGoogleMap
and ConsumerController
.
-
Get
ConsumerGoogleMapfrom eitherConsumerMapFragmentorConsumerMapView, both of which asynchronously returnConsumerGoogleMapinConsumerMapReadyCallback.ConsumerGoogleMapis a wrapper class for theGoogleMapclass. It uses an API equivalent toGoogleMapso that your app can interact with the map. In this way, your app can interact seamlessly with the same underlying Google map. For example,GoogleMaponly allows a single callback registration, butConsumerGoogleMapsupports dual registered callbacks. These callbacks let your app register callbacks that are called sequentially. -
Get
ConsumerControllerfromConsumerGoogleMapingetConsumerController().ConsumerControllerprovides access to trip sharing features such as monitor trips, control trip status, and set locations.
For how to add ConsumerGoogleMap
and ConsumerController
to your app in
Java and Kotlin, see the following examples.
Java
private
ConsumerGoogleMap
consumerGoogleMap
;
private
ConsumerController
consumerController
;
private
ConsumerMapView
consumerMapView
;
consumerMapView
.
getConsumerGoogleMapAsync
(
new
ConsumerMapReadyCallback
()
{
@Override
public
void
onConsumerMapReady
(
@NonNull
ConsumerGoogleMap
consumerMap
)
{
consumerGoogleMap
=
consumerMap
;
consumerController
=
consumerMap
.
getConsumerController
();
}
},
this
,
null
);
Kotlin
var
consumerGoogleMap
:
ConsumerGoogleMap
var
consumerController
:
ConsumerController
val
consumerMapView
=
findViewById
(
R
.
id
.
consumer_map_view
)
as
ConsumerMapView
consumerMapView
.
getConsumerGoogleMapAsync
(
object
:
ConsumerMapReadyCallback
()
{
override
fun
onConsumerMapReady
(
consumerMap
:
ConsumerGoogleMap
)
{
consumerGoogleMap
=
consumerMap
consumerController
=
consumerMap
.
getConsumerController
()
},
/* fragmentActivity= */
this
,
/* googleMapOptions= */
null
,
}
)
Step 3: Add support for Android vector graphics
If your app design requires support for vector graphics, then add support for Android devices and vector drawables using these steps:
- Add the following code to your Activity. This codes extends
AppCompatActivityto use the Vector drawables in the Consumer SDK.
Java
// ...
import
android.support.v7.app.AppCompatActivity
;
// ...
public
class
ConsumerTestActivity
extends
AppCompatActivity
{
// ...
}
Kotlin
// ...
import
android.support.v7.app.AppCompatActivity
// ...
class
ConsumerTestActivity
:
AppCompatActivity
()
{
// ...
}

