We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
The documentation of HarmonyOS 5.0.1 (API 13) you're currently viewing is archived and no longer updated. You are advised to switch to the latest version.
GuidesApplication ServicesMap KitMap InteractionMap Location Change

Map Location Change

Use Cases

The Huawei map's movement functionality is implemented by simulating camera movements. You can control the visible region of a map by changing the camera's position, as shown in the following figure.

This section describes the attributes of the camera and how to move the camera.

Figure 1 Before moving the camera
Figure 2 After moving the camera

API Description

You can use the moveCamera(update: CameraUpdate), animateCamera(update: CameraUpdate, duration?: number), and animateCameraStatus (update: CameraUpdate, duration?: number) methods in the map namespace to move a map camera. The following describes how to create the input parameter CameraUpdate.

Expand

API

Description

zoomTo(zoom: number): CameraUpdate

Sets the zoom level of a map.

zoomOut(): CameraUpdate

Zooms out on a map by 1 level.

zoomIn(): CameraUpdate

Zooms in on a map by 1 level.

zoomBy(amount: number, focus?: mapCommon.MapPoint): CameraUpdate

Zooms in or out on a map by a specified value with the specified pixel point as the center.

scrollBy(x: number, y: number): CameraUpdate

Moves the map center point by pixel.

newLatLngBounds(bounds: mapCommon.LatLngBounds, padding?: number): CameraUpdate

Sets the longitude and latitude ranges of a map, and the padding between the map region and map edges.

newLatLngBounds(bounds: mapCommon.LatLngBounds, width: number, height: number, padding: number): CameraUpdate

Sets the longitude and latitude ranges of a map, the height and width of the longitude and latitude rectangle range, and the padding between the map region and map edges.

newLatLngBounds(bounds: mapCommon.LatLngBounds, padding: mapCommon.Padding): CameraUpdate

Sets the longitude and latitude ranges of a map, and the padding between the map region and the four map edges.

newLatLng(latLng: mapCommon.LatLng, zoom?: number): CameraUpdate

Sets the map center and zoom level.

newCameraPosition(cameraPosition: mapCommon.CameraPosition): CameraUpdate

Updates the map status.

Development Procedure

Moving the Camera

  1. Initialize the map and obtain an object of the map operation class MapComponentController. For details, please refer to Displaying a Map.
  2. Import required modules.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { MapComponent, mapCommon, map } from '@kit.MapKit';
  3. Call the moveCamera, animateCamera, and animateCameraStatus methods of MapComponentController to move the map camera.

    You can move the map camera in animation or non-animation mode.

    • When moving the camera in animation mode, you can set the duration of the animation.
    • Camera movement in non-animation mode is transient.
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Create a CameraUpdate object.
    2. let cameraPosition: mapCommon.CameraPosition = {
    3. target: {
    4. latitude: 32.0,
    5. longitude: 118.0
    6. },
    7. zoom: 10,
    8. tilt: 0,
    9. bearing: 0
    10. };
    11. let cameraUpdate = map.newCameraPosition(cameraPosition);
    12. // Move the map camera in non-animation mode.
    13. this.mapController.moveCamera(cameraUpdate);
    14. // Move the map camera in animation mode.
    15. this.mapController.animateCamera(cameraUpdate, 1000);
    16. // Move the map camera in animation mode and return the animation result.
    17. let animateResult = await this.mapController.animateCameraStatus(cameraUpdate, 1000);
    Figure 3 Before moving the camera
    Figure 4 After moving the camera
  4. You can also create a CameraUpdate object using either of the following methods:
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Method 1: Increase the camera zoom level by 1 and retain other attribute settings.
    2. let cameraUpdate = map.zoomIn();
    3. // Method 2: Decrease the camera zoom level by 1 and retain other attribute settings.
    4. let cameraUpdate1 = map.zoomOut();
    5. // Method 3: Set the camera zoom level to a specified value and retain other attribute settings.
    6. let zoom1 = 8.0;
    7. let cameraUpdate2 = map.zoomTo(zoom1);
    8. // Method 4:
    9. // a. Increase or decrease the camera zoom level by a specified value.
    10. // b. Move the camera to the specified center point and increase or decrease the camera zoom level.
    11. // The left vertex of the screen is used as the (0, 0) point. A positive value of positionX indicates that the visible region moves rightwards, while a negative value indicates that the visible region moves leftwards.
    12. // A positive value of positionY indicates that the visible region moves downwards, while a negative value indicates that the visible region moves upwards.
    13. let point: mapCommon.MapPoint = {
    14. positionX: 31,
    15. positionY: 118
    16. };
    17. let amount = 2.0;
    18. let cameraUpdate3 = map.zoomBy(amount, point);
    19. // Method 5: Set the longitude, latitude, and map zoom level of the camera.
    20. // a. Specify only the longitude and latitude of the camera to move the center point.
    21. // b. Specify the longitude, latitude, and map zoom level of the camera. You can call this API to move the camera to the center point and zoom in or out the camera.
    22. let latLng: mapCommon.LatLng = {
    23. latitude: 31.5,
    24. longitude: 118.9
    25. };
    26. let zoom2 = 10;
    27. let cameraUpdate4 = map.newLatLng(latLng, zoom2);
    28. // Method 6: Set the visible region of the camera.
    29. let latLngBounds: mapCommon.LatLngBounds = {
    30. northeast: {
    31. latitude: 32.5,
    32. longitude: 119.9
    33. },
    34. southwest: {
    35. latitude: 31.5,
    36. longitude: 118.9
    37. }
    38. };
    39. // Set the longitude and latitude ranges of the map, and set the padding between the map region and map edges to 5 pixels.
    40. let cameraUpdate5 = map.newLatLngBounds(latLngBounds, 5);
    41. // Method 7: Set the visible region of the camera.
    42. // Set the longitude and latitude ranges of the map. Set the width and height of the longitude and latitude rectangle to 9 pixels and 16 pixels respectively, and set the padding between the map area and edges to 5 pixels.
    43. let cameraUpdate6 = map.newLatLngBounds(latLngBounds, 9, 16, 5);
    44. // Method 8: Set the longitude and latitude range of the map. Set the padding between the map area and the left, top, right, and bottom edges to 5, 6, 7, and 8 pixels, respectively.
    45. let paddings: mapCommon.Padding = {
    46. left:5,
    47. top: 6,
    48. right: 7,
    49. bottom: 8
    50. };
    51. let cameraUpdate7 = map.newLatLngBounds(latLngBounds, paddings);
    52. // Method 9: Scroll the camera by a specified number of pixels.
    53. let x = 100.0;
    54. let y = 100.0;
    55. let cameraUpdate8 = map.scrollBy(x, y);

Setting the Preferred Minimum and Maximum Camera Zoom Levels

Collapse
Word wrap
Dark theme
Copy code
  1. // Set the preferred minimum zoom level. The value ranges from 2 to 20.
  2. this.mapController.setMinZoom(6);
  3. // Set the preferred maximum zoom level. The value ranges from 2 to 20.
  4. this.mapController.setMaxZoom(14);

Setting the Bounds of the Map Camera

Map Kit allows you to set the map camera bounds. You can call the setLatLngBounds (bounds: mapCommon.LatLngBounds) API to specify 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.

Collapse
Word wrap
Dark theme
Copy code
  1. let bounds: mapCommon.LatLngBounds = {
  2. northeast: {
  3. latitude: 31,
  4. longitude: 118
  5. },
  6. southwest: {
  7. latitude: 30,
  8. longitude: 113
  9. }
  10. };
  11. this.mapController.setLatLngBounds(bounds);