目录

1 Overview

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.

2 Map Camera

2.1 Camera Attributes

The camera has four attributes.

AttributeDescription
targetLongitude and latitude of the map center.
bearingAngle of a map rotated from the north in a clockwise direction.
tiltAngle of the camera from the nadir (directly facing the Earth), in degrees.
zoomZoom level near the center of the screen.

2.2 Camera Movement

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:

  1. // Method 1: Increase the camera zoom level by 1 and retain other attribute settings.
  2. CameraUpdate cameraUpdate = CameraUpdateFactory.zoomIn();
  3. // Method 2: Decrease the camera zoom level by 1 and retain other attribute settings.
  4. CameraUpdate cameraUpdate1 = CameraUpdateFactory.zoomOut();
  5. // Method 3: Set the camera zoom level to a specified value and retain other attribute settings.
  6. float zoom = 8.0f;
  7. CameraUpdate cameraUpdate2 = CameraUpdateFactory.zoomTo(zoom);
  8. // Method 4: Increase or decrease the camera zoom level by a specified value.
  9. float amount = 2.0f;
  10. CameraUpdate cameraUpdate3 = CameraUpdateFactory.zoomBy(amount);
  11. // Method 5: Move the camera to the specified center point and increase or decrease the camera zoom level by a specified value.  
  12. // Set the position of a point on the screen.
  13. Point point = new Point(31, 118);
  14. float amount = 2.0f;
  15. CameraUpdate cameraUpdate4 = CameraUpdateFactory.zoomBy(amount, point);
  16. // Method 6: Set the latitude and longitude of the camera and retain other attribute settings.
  17. LatLng latLng1 = new LatLng(31.5, 118.9);
  18. CameraUpdate cameraUpdate5 = CameraUpdateFactory.newLatLng(latLng1);
  19. // Method 7: Set the visible region and padding.
  20. int padding = 100;
  21. LatLng latLng1 = new LatLng(31.5, 118.9);
  22. LatLng latLng2 = new LatLng(32.5, 119.9);
  23. LatLngBounds latLngBounds = new LatLngBounds(latLng1, latLng2);
  24. CameraUpdate cameraUpdate6 = CameraUpdateFactory.newLatLngBounds(latLngBounds, padding);
  25. // Method 8: Set the center point and zoom level of the camera.
  26. float zoom = 0.0f;
  27. LatLng latLng2 = new LatLng(32.5, 119.9);
  28. CameraUpdate cameraUpdate7 = CameraUpdateFactory.newLatLngZoom(latLng2, zoom);
  29. // Method 9: Scroll the camera by specified pixels.
  30. float x = 100.0f;
  31. float y = 100.0f;
  32. CameraUpdate cameraUpdate8 = CameraUpdateFactory.scrollBy(x, y);
  33. // Method 10: Specify the camera position.
  34. // Set the tilt.
  35. float tilt = 2.2f;
  36. // Set the bearing.
  37. float bearing = 31.5f;
  38. CameraPosition cameraPosition = new CameraPosition(latLng1,zoom,tilt,bearing);
  39. 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.

  1. // Move the map camera in animation mode.
  2. hMap.animateCamera(CameraUpdate);
  3. // Move the map camera in animation mode and set the API to be called back when the animation stops.
  4. hMap.animateCamera(CameraUpdate, CancelableCallback);
  5. // Move the map camera in animation mode, and set the animation duration and API to be called back when the animation stops.  
  6. hMap.animateCamera(CameraUpdate, int, CancelableCallback);
  7. // Move the map camera in non-animation mode.
  8. hMap.moveCamera(CameraUpdate);

Methods of setting the preferred minimum and maximum camera zoom levels are as follows:

  1. // Set the preferred minimum zoom level.
  2. hMap.setMinZoomPreference(float);
  3. // Set the preferred maximum zoom level.
  4. hMap.setMaxZoomPreference(float);
  5. // Reset the maximum and minimum zoom levels.
  6. hMap.resetMinMaxZoomPreference();


Figure 1: Screen before specifying the camera position



Figure 2: Screen after specifying the camera position


2.3 Area Constraint

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.

3 UI Controls and Gestures

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.

  1. // Specify whether to enable the zoom controls.
  2. 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.

  1. // Specify whether to enable the compass.
  2. 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.

  1. // Enable the my-location layer.
  2. hMap.setMyLocationEnabled(true);
  3. // Enable the function of displaying the my-location icon.
  4. hMap.getUiSettings().setMyLocationButtonEnabled(true);

Map Gestures

You can use a UiSettings object to enable or disable a map gesture.

  1. // Specify whether to enable the zoom gestures.
  2. hMap.getUiSettings().setZoomGesturesEnabled(true);
  1. // Specify whether to enable the scroll gestures.
  2. hMap.getUiSettings().setScrollGesturesEnabled(true);
  1. // Specify whether to enable the tilt gestures.
  2. hMap.getUiSettings().setTiltGesturesEnabled(true);
  1. // Specify whether to enable the rotate gestures.
  2. hMap.getUiSettings().setRotateGesturesEnabled(true);

4 Event Listening

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.

4.1 Map Event Listening

Tap event:

  1. hMap.setOnMapClickListener(new HuaweiMap.OnMapClickListener() {
  2.    @Override
  3.    public void onMapClick(LatLng latLng) {
  4.        Toast.makeText(getApplicationContext(), "onMapClick:" + latLng.toString(), Toast.LENGTH_SHORT).show();
  5.    }
  6. });

Long press event:

  1. hMap.setOnMapLongClickListener(new HuaweiMap.OnMapLongClickListener() {
  2.    @Override
  3.    public void onMapLongClick(LatLng latLng) {
  4.        Toast.makeText(getApplicationContext(), "onMapLongClick:" + latLng.toString(), Toast.LENGTH_SHORT).show();
  5.    }
  6. });

4.2 Camera Moving Event Listening

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.

  1. @Override
  2. public void onMapReady(HuaweiMap huaweiMap) {
  3.    Log.i(TAG, "onMapReady: ");
  4.    hMap = huaweiMap;
  5.    hMap.setMyLocationEnabled(true);
  6.    hMap.setOnCameraMoveStartedListener(this);
  7.    hMap.setOnCameraIdleListener(this);
  8.    hMap.setOnCameraMoveListener(this);
  9.    hMap.setOnMapLoadedCallback(new HuaweiMap.OnMapLoadedCallback() {
  10.        @Override
  11.        public void onMapLoaded() {
  12.            Log.i(TAG, "onMapLoaded: successful");
  13.        }
  14.    });
  15. }
  16. // Callback when the camera starts moving
  17. @Override
  18. public void onCameraMoveStarted(int reason) {
  19.    Log.i(TAG, "onCameraMoveStarted: successful");
  20. }
  21. // Callback when the camera stops moving
  22. @Override
  23. public void onCameraIdle() {
  24.    Log.i(TAG, "onCameraIdle: successful");
  25. }
  26. // Callback during camera moving
  27. @Override
  28. public void onCameraMove() {
  29.    Log.i(TAG, "onCameraMove: successful");
  30. }

4.3 Other Event Listening

Marker tap event:

  1. hMap.setOnMarkerClickListener(new HuaweiMap.OnMarkerClickListener() {
  2.    @Override
  3.    public boolean onMarkerClick(Marker marker) {
  4.        Toast.makeText(getApplicationContext(), "onMarkerClick:" + marker.getTitle(), Toast.LENGTH_SHORT).show();
  5.        return false;
  6.    }
  7. });

Information window tap event:

  1. hMap.setOnInfoWindowClickListener(new HuaweiMap.OnInfoWindowClickListener() {
  2.    @Override
  3.    public void onInfoWindowClick(Marker marker) {
  4.        Toast.makeText(getApplicationContext(), "onInfoWindowClick:" + marker.getTitle(), Toast.LENGTH_SHORT).show();
  5.    }
  6. });

My-location button tap event:

  1. hMap.setOnMyLocationButtonClickListener(new HuaweiMap.OnMyLocationButtonClickListener() {
  2.    @Override
  3.    public boolean onMyLocationButtonClick() {
  4.        Toast.makeText(getApplicationContext(), "MyLocation button clicked", Toast.LENGTH_SHORT).show();
  5.        return false;
  6.    }
  7. });