Skip to main content

Trimble layers

Contents

Add specialized layers to the map, ranging from weather alerts to 3D buildings.

trimble-layers

activity_sample_trimble_layers.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.trimblemaps.mapsdk.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <Button
            android:onClick="onClickToggleTrimbleLayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Traffic" />

        <Button
            android:onClick="onClickToggleTrimbleLayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3D Buildings" />

        <Button
            android:onClick="onClickToggleTrimbleLayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="POIs" />
        <Button
            android:onClick="onClickToggleTrimbleLayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Weather" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

SampleTrimbleLayersActivity.java

public class SampleTrimbleLayersActivity extends AppCompatActivity {

    private MapView mapView;
    private TrimbleMapsMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Authorize the api key for the session.
        // .apiKey() requires your Trimble Maps API key
        TrimbleMapsAccount trimbleMapsAccount = TrimbleMapsAccount.builder()
                .apiKey(getString(R.string.API_KEY))
                .addLicensedFeature(LicensedFeature.MAPS_SDK)
                .build();

        // Initialize the session
        TrimbleMapsAccountManager.initialize(trimbleMapsAccount);
        TrimbleMapsAccountManager.awaitInitialization();

        // Get an instance of the map, done before the layout is set.
        TrimbleMaps.getInstance(this);
        setContentView(R.layout.activity_sample_trimble_layers);

        // Set up the MapView from the layout
        mapView = (MapView) findViewById(R.id.mapView);
        // the onMapReadyCallback is fired when the map is ready to be worked with
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(@NonNull TrimbleMapsMap trimbleMapsMap) {
                // The TrimbleMapsMap object is created, now a style can be applied to render a map.
                map = trimbleMapsMap;
                CameraPosition position = new CameraPosition.Builder()
                        .target(new LatLng(40.7584766,-73.9840227))
                        .zoom(13)
                        .build();
                map.setCameraPosition(position);

                map.setStyle(Style.MOBILE_DEFAULT);
            }
        });

    }

    // Function called when a button in the layout is pressed.
    public void onClickToggleTrimbleLayer(View view) {
        // Simple switch statement on the button's text, layers will toggle depending on what was pressed
        // The button's label is used for the switch statement for readability purposes.
        // Toggle's are used here, but these layers can also be implicitly set with boolean values.
        // E.g. map.setTrafficVisibility(true);
        String buttonText = ((Button) view).getText().toString().toLowerCase();
        switch (buttonText) {
            case "traffic":

                map.toggleTrafficVisibility();
                break;
            case "3d buildings":
                map.toggle3dBuildingVisibility();
                break;
            case "pois":
                map.togglePoiVisibility();
                break;
            case "weather":
                map.toggleWeatherAlertVisibility();
                break;
        }
    }
}
Last updated September 2, 2022.
Contents