By controlling the built-in UI control display and allowable gestures, you can customize modes for users to interact with the map. For details, please refer to files such as CameraDemoActivity.java, GestureDemoActivity.java, and GestureControlsDemoActivity.java in the sample code project. Click here to download sample code.
The camera has four attributes.
| Attribute | Description |
| target | Longitude and latitude of the map center. |
| bearing | Angle of a map rotated from the north in a clockwise direction. |
| tilt | Angle of the camera from the nadir (directly facing the Earth), in degrees. |
| zoom | Zoom level near the center of the screen. |
Huawei maps are moved by simulating camera movement. You can control the visible region of a map by changing the camera's position. To change the camera's position, create different types of CameraUpdate objects using the CameraUpdateFactory class, and use these objects to move the camera.
Methods of creating a CameraUpdate object are as follows:
- // Method 1: Increase the camera zoom level by 1 and retain other attribute settings.
- CameraUpdate cameraUpdate = CameraUpdateFactory.zoomIn();
- // Method 2: Decrease the camera zoom level by 1 and retain other attribute settings.
- CameraUpdate cameraUpdate1 = CameraUpdateFactory.zoomOut();
- // Method 3: Set the camera zoom level to a specified value and retain other attribute settings.
- float zoom = 8.0f;
- CameraUpdate cameraUpdate2 = CameraUpdateFactory.zoomTo(zoom);
- // Method 4: Increase or decrease the camera zoom level by a specified value.
- float amount = 2.0f;
- CameraUpdate cameraUpdate3 = CameraUpdateFactory.zoomBy(amount);
- // Method 5: Move the camera to the specified center point and increase or decrease the camera zoom level by a specified value.
- // Set the position of a point on the screen.
- Point point = new Point(31, 118);
- float amount = 2.0f;
- CameraUpdate cameraUpdate4 = CameraUpdateFactory.zoomBy(amount, point);
- // Method 6: Set the latitude and longitude of the camera and retain other attribute settings.
- LatLng latLng1 = new LatLng(31.5, 118.9);
- CameraUpdate cameraUpdate5 = CameraUpdateFactory.newLatLng(latLng1);
- // Method 7: Set the visible region and padding.
- int padding = 100;
- LatLng latLng1 = new LatLng(31.5, 118.9);
- LatLng latLng2 = new LatLng(32.5, 119.9);
- LatLngBounds latLngBounds = new LatLngBounds(latLng1, latLng2);
- CameraUpdate cameraUpdate6 = CameraUpdateFactory.newLatLngBounds(latLngBounds, padding);
- // Method 8: Set the center point and zoom level of the camera.
- float zoom = 0.0f;
- LatLng latLng2 = new LatLng(32.5, 119.9);
- CameraUpdate cameraUpdate7 = CameraUpdateFactory.newLatLngZoom(latLng2, zoom);
- // Method 9: Scroll the camera by specified pixels.
- float x = 100.0f;
- float y = 100.0f;
- CameraUpdate cameraUpdate8 = CameraUpdateFactory.scrollBy(x, y);
- // Method 10: Specify the camera position.
- // Set the tilt.
- float tilt = 2.2f;
- // Set the bearing.
- float bearing = 31.5f;
- CameraPosition cameraPosition = new CameraPosition(latLng1,zoom,tilt,bearing);
- CameraUpdate cameraUpdate9 = CameraUpdateFactory.newCameraPosition(cameraPosition);
Methods of changing the camera position are as follows:
You can move the map camera in animation or non-animation mode.
When moving the map camera in animation mode, you can set the animation duration and API to be called back when the animation stops. By default, the animation duration is 250 ms.
Camera movement in non-animation mode is transient.
- // Move the map camera in animation mode.
- hMap.animateCamera(CameraUpdate);
- // Move the map camera in animation mode and set the API to be called back when the animation stops.
- hMap.animateCamera(CameraUpdate, CancelableCallback);
- // Move the map camera in animation mode, and set the animation duration and API to be called back when the animation stops.
- hMap.animateCamera(CameraUpdate, int, CancelableCallback);
- // Move the map camera in non-animation mode.
- hMap.moveCamera(CameraUpdate);
Methods of setting the preferred minimum and maximum camera zoom levels are as follows:
- // Set the preferred minimum zoom level.
- hMap.setMinZoomPreference(float);
- // Set the preferred maximum zoom level.
- hMap.setMaxZoomPreference(float);
- // Reset the maximum and minimum zoom levels.
- hMap.resetMinMaxZoomPreference();

Figure 1: Screen before specifying the camera position

Figure 2: Screen after specifying the camera position
HMS Core Map SDK allows you to set the map camera bounds. You can call the setLatLngBoundsForCameraTarget(LatLngBounds) API to specify a LatLngBounds to constrain the camera target so it will not get out of the specified bounds when users scroll the map camera. If this parameter is set to null, the map camera bounds will be cleared.
You can use the getUiSettings() method in the HuaweiMap class to obtain a UiSettings object that controls the visibility of UI controls. UI controls include the zoom controls, compass, my-location icon, and map gestures.
Zoom Controls
The HMS Core Map SDK provides built-in zoom controls, which are enabled by default.
- // Specify whether to enable the zoom controls.
- hMap.getUiSettings().setZoomControlsEnabled(false);
Compass
The HMS Core Map SDK provides the compass function. By default, a compass is displayed in the upper right corner of the map. If the compass is enabled and the map is not oriented to the due north, a compass icon is displayed in the upper right corner of the map. A user can tap the compass icon to rotate the map to be oriented to the due north. If the compass is enabled and the map is oriented to the due north, the compass icon is hidden. If the compass is disabled, the compass icon will never be displayed.
- // Specify whether to enable the compass.
- hMap.getUiSettings().setCompassEnabled(false);
My-Location Icon
Before enabling the my-location function, ensure that your app has the android.permission.ACCESS_FINE_LOCATION and android.permission.ACCESS_COARSE_LOCATION permission and the my-location layer has been enabled. The my-location icon is displayed on a map only after being enabled.
- // Enable the my-location layer.
- hMap.setMyLocationEnabled(true);
- // Enable the function of displaying the my-location icon.
- hMap.getUiSettings().setMyLocationButtonEnabled(true);
Map Gestures
You can use a UiSettings object to enable or disable a map gesture.
Zoom gestures: A user can double-tap the map to increase the zoom level by 1 (zoom-in), or tap the map with two fingers to decrease the zoom level by 1 (zoom-out).
- // Specify whether to enable the zoom gestures.
- hMap.getUiSettings().setZoomGesturesEnabled(true);
Scroll gestures: A user can drag the map to move it.
- // Specify whether to enable the scroll gestures.
- hMap.getUiSettings().setScrollGesturesEnabled(true);
Tilt gestures: A user can move two fingers up or down on a map to change the tilt angle.
- // Specify whether to enable the tilt gestures.
- hMap.getUiSettings().setTiltGesturesEnabled(true);
Rotate gestures: A user can rotate a map with two fingers.
- // Specify whether to enable the rotate gestures.
- hMap.getUiSettings().setRotateGesturesEnabled(true);
This section describes how to set listeners for map tap and long press events, camera moving events, and other events such as my-location icon tap events.
Tap event:
- hMap.setOnMapClickListener(new HuaweiMap.OnMapClickListener() {
- @Override
- public void onMapClick(LatLng latLng) {
- Toast.makeText(getApplicationContext(), "onMapClick:" + latLng.toString(), Toast.LENGTH_SHORT).show();
- }
- });
Long press event:
- hMap.setOnMapLongClickListener(new HuaweiMap.OnMapLongClickListener() {
- @Override
- public void onMapLongClick(LatLng latLng) {
- Toast.makeText(getApplicationContext(), "onMapLongClick:" + latLng.toString(), Toast.LENGTH_SHORT).show();
- }
- });
After a listener is set for camera moving events, the application layer can listen on the camera moving status.
When the camera starts to move, the onCameraMoveStarted() method of HuaweiMap.OnCameraMoveStartedListener is called.
When the camera moves or a user interacts with the map, the onCameraMove() method of HuaweiMap.OnCameraMoveListener will be called for multiple times.
When the camera stops moving, the onCameraIdle() method of HuaweiMap.OnCameraIdleListener is called.
- @Override
- public void onMapReady(HuaweiMap huaweiMap) {
- Log.i(TAG, "onMapReady: ");
- hMap = huaweiMap;
- hMap.setMyLocationEnabled(true);
- hMap.setOnCameraMoveStartedListener(this);
- hMap.setOnCameraIdleListener(this);
- hMap.setOnCameraMoveListener(this);
- hMap.setOnMapLoadedCallback(new HuaweiMap.OnMapLoadedCallback() {
- @Override
- public void onMapLoaded() {
- Log.i(TAG, "onMapLoaded: successful");
- }
- });
- }
- // Callback when the camera starts moving
- @Override
- public void onCameraMoveStarted(int reason) {
- Log.i(TAG, "onCameraMoveStarted: successful");
- }
- // Callback when the camera stops moving
- @Override
- public void onCameraIdle() {
- Log.i(TAG, "onCameraIdle: successful");
- }
- // Callback during camera moving
- @Override
- public void onCameraMove() {
- Log.i(TAG, "onCameraMove: successful");
- }
Marker tap event:
- hMap.setOnMarkerClickListener(new HuaweiMap.OnMarkerClickListener() {
- @Override
- public boolean onMarkerClick(Marker marker) {
- Toast.makeText(getApplicationContext(), "onMarkerClick:" + marker.getTitle(), Toast.LENGTH_SHORT).show();
- return false;
- }
- });
Information window tap event:
- hMap.setOnInfoWindowClickListener(new HuaweiMap.OnInfoWindowClickListener() {
- @Override
- public void onInfoWindowClick(Marker marker) {
- Toast.makeText(getApplicationContext(), "onInfoWindowClick:" + marker.getTitle(), Toast.LENGTH_SHORT).show();
- }
- });
My-location button tap event:
- hMap.setOnMyLocationButtonClickListener(new HuaweiMap.OnMyLocationButtonClickListener() {
- @Override
- public boolean onMyLocationButtonClick() {
- Toast.makeText(getApplicationContext(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
- return false;
- }
- });