Skip to main content

Map styles

Contents

Set the map to night, day, or satellite.

changing-styles

activity_sample_change_styles.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="onClickChangeStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Day" />

        <Button
            android:onClick="onClickChangeStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Night" />

        <Button
            android:onClick="onClickChangeStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Satellite" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

SampleChangeStylesActivity.java

public class SampleChangeStylesActivity extends AppCompatActivity implements Style.OnStyleLoaded{

    private MapView mapView;
    private TrimbleMapsMap map;
    private String chosenStyle = "Day Style"; // Used for toasts

    @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_change_styles);

        // 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.
                // Set the initial camera position for a starting location
                map = trimbleMapsMap;
                CameraPosition position = new CameraPosition.Builder()
                        .target(new LatLng(40.7584766,-73.9840227))
                        .zoom(13)
                        .build();
                map.setCameraPosition(position);

                // Set the style, the callback is implemented into the class.
                map.setStyle(Style.MOBILE_DAY, SampleChangeStylesActivity.this);
            }
        });
    }

    // When the buttons in the layout are clicked, this function is called to change the style
    public void onClickChangeStyle(View view) {
        // Simple switch statement on the button's text, style will change depending on it.
        String buttonText = ((Button) view).getText().toString();
        switch (buttonText) {
            case "Satellite":
                chosenStyle = "Satellite Style";
                map.setStyle(Style.SATELLITE, this);
                break;
            case "Day":
                chosenStyle = "Day Style";
                map.setStyle(Style.MOBILE_DAY, this);
                break;
            case "Night":
                chosenStyle = "Night Style";
                map.setStyle(Style.MOBILE_NIGHT, this);
                break;
        }
    }

    // Implemented onStyleLoaded to the class. When the style is changed a small toast appears to
    // confirm the choice.
    @Override
    public void onStyleLoaded(@NonNull Style style) {
        Toast toast = Toast.makeText(SampleChangeStylesActivity.this, chosenStyle, Toast.LENGTH_SHORT);
        toast.show();
    }
}
Last updated January 26, 2023.
Contents