目录

Map Attribute Settings

You can set initial map attributes using the layout file (for example, activity_main.xml) or the code file, and dynamically update them later.

Setting Attributes Using the Layout File

The Map SDK defines a group of map attributes in the layout file for SupportMapFragment and MapView. You can directly use these attributes in the layout file to set initial map attributes.

Before using these attributes, ensure that you have declared the following namespace:

xmlns:map="http://schemas.android.com/apk/res-auto"

After declaring the namespace, you can add the following attributes as the initial attributes of a map:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mapfragment_mapfragmentdemo"
    class="com.huawei.hms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraBearing="45"
    map:cameraMaxZoomPreference="13"
    map:cameraMinZoomPreference="3"
    map:cameraTargetLat="48.893478"
    map:cameraTargetLng="2.334595"
    map:cameraTilt="20"
    map:cameraZoom="10"
    map:latLngBoundsNorthEastLatitude="49.893478"
    map:latLngBoundsNorthEastLongitude="1.334595"
    map:latLngBoundsSouthWestLatitude="47.893478"
    map:latLngBoundsSouthWestLongitude="3.334595"
    map:liteMode="false"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="false"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="false"
    map:uiZoomGestures="true"
    map:useViewLifecycle="true"
    map:zOrderOnTop="true"
    map:dark="true" />

Setting Attributes Using the Code File

You can also set initial attributes for a map in the code file and update these attributes for changing the camera status. All map attributes in the layout file can be set in the code file.

The sample code is as follows:
Java
// Declare a SupportMapFragment object.
SupportMapFragment mSupportMapFragment;
// Set initial camera attributes.
CameraPosition cameraPosition =
    CameraPosition.builder().target(new LatLng(48.893478, 2.334595)).zoom(10).bearing(45).tilt(20).build();
// Construct the target area of the camera.
LatLng southwest = new LatLng(47.893478, 3.334595);
LatLng northeast = new LatLng(49.893478, 1.334595);
LatLngBounds latLngBounds = new LatLngBounds(southwest, northeast);

HuaweiMapOptions options = new HuaweiMapOptions();
        // Set the map type. The options are none, normal, and terrain.
 options.mapType(HuaweiMap.MAP_TYPE_NORMAL)
        // Set camera attributes.
        .camera(cameraPosition)
        // Set whether to enable the zoom controls. They are enabled by default.
        .zoomControlsEnabled(false)
        // Set whether to enable the compass. It is enabled by default.
        .compassEnabled(true)
        // Set whether to enable zoom gestures. They are enabled by default.
        .zoomGesturesEnabled(true)
        // Set whether to enable scroll gestures. They are enabled by default.
        .scrollGesturesEnabled(true)
        // Set whether to enable rotation gestures. They are enabled by default.
        .rotateGesturesEnabled(false)
        // Set whether to enable tilt gestures. They are enabled by default.
        .tiltGesturesEnabled(true)
        // Set whether to place the map view on the top of the map window. The default value is false.
        .zOrderOnTop(true)
        // Set whether to bind the map lifecycle to SupportMapFragment or the view of SupportMapFragment. The default value is true.
        .useViewLifecycleInFragment(true)
        // Set whether to enable the lite mode for the map. It is disabled by default.
        .liteMode(false)
        // Set the preferred minimum zoom level.
        .minZoomPreference(3)
        // Set the preferred maximum zoom level.
        .maxZoomPreference(13)
        // Set an area to constrain the camera target so that the camera target does not move outside the bounds when a user scrolls the map camera.
        .latLngBoundsForCameraTarget(latLngBounds)
        // Set whether to enable the dark mode.
        .dark(true);
        
mSupportMapFragment = SupportMapFragment.newInstance(options);
Kotlin
// Declare a SupportMapFragment object.
val mSupportMapFragment: SupportMapFragment
// Set initial camera attributes.
val cameraPosition = CameraPosition.builder().target(LatLng(48.893478, 2.334595)).zoom(10f).bearing(45f).tilt(20f).build()
 
// Construct the target area of the camera.
val southwest = LatLng(47.893478, 3.334595)
val northeast = LatLng(49.893478, 1.334595)
val latLngBounds = LatLngBounds(southwest, northeast)
 
val options = HuaweiMapOptions()
        // Set the map type. The options are none, normal, and terrain.
 options.mapType(HuaweiMap.MAP_TYPE_NORMAL)
        // Set camera attributes.
        .camera(cameraPosition)
        // Set whether to enable the zoom function. It is enabled by default.
        .zoomControlsEnabled(false)
        // Set whether to enable the compass. It is enabled by default.
        .compassEnabled(true)
        // Set whether to enable zoom gestures. They are enabled by default.
        .zoomGesturesEnabled(true)
        // Set whether to enable scroll gestures. They are enabled by default.
        .scrollGesturesEnabled(true)
        // Set whether to enable rotation gestures. They are enabled by default.
        .rotateGesturesEnabled(false)
        // Set whether to enable tilt gestures. They are enabled by default.
        .tiltGesturesEnabled(true)
        // Set whether to place the map view on the top of the map window. The default value is true.
        .zOrderOnTop(true)
        // Set whether to bind the map lifecycle to SupportMapFragment or the view of SupportMapFragment. The default value is true.
        .useViewLifecycleInFragment(true)
        // Set whether to enable the lite mode for the map. It is disabled by default.
        .liteMode(false)
        // Set the preferred minimum zoom level.
        .minZoomPreference(3f)
        // Set the preferred maximum zoom level.
        .maxZoomPreference(13f)
        // Set an area to constrain the camera target so that the camera target does not move outside the bounds when a user scrolls the map camera.
        .latLngBoundsForCameraTarget(latLngBounds)
        // Set whether to enable the dark mode.
        .dark(true)
        
mSupportMapFragment = SupportMapFragment.newInstance(options)

When creating SupportMapFragment, you need to associate it with the lifecycle of Fragment, that is, the parameter value of the HuaweiMapOptions.useViewLifecycleInFragment(boolean isUseViewLifecycleInFragment) method must be true.