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
GuidesMap KitAndroidMap InteractionMap Camera

Map Camera

Camera Attributes

The following table describes the camera attributes.

Expand

Attribute

Description

target

Longitude and latitude of the map center.

bearing

Angle of a map rotated from the north in the 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.

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

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

Methods of Changing the Camera Position

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.

The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  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, durationMs, cancelableCallback);
  7. // Move the map camera in non-animation mode.
  8. hMap.moveCamera(cameraUpdate);
Collapse
Word wrap
Dark theme
Copy code
  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, durationMs, 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

The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. // Set the preferred minimum zoom level. The value ranges from 3 to 20.
  2. hMap.setMinZoomPreference(
    3
    );
  3. // Set the preferred maximum zoom level. The value ranges from 3 to 20.
  4. hMap.setMaxZoomPreference(
    14
    );
  5. // Reset the preferred maximum and minimum zoom levels.
  6. hMap.resetMinMaxZoomPreference();
Collapse
Word wrap
Dark theme
Copy code
  1. // Set the preferred minimum zoom level.
  2. hMap.setMinZoomPreference(
    3f
    )
  3. // Set the preferred maximum zoom level.
  4. hMap.setMaxZoomPreference(
    14f
    )
  5. // Reset the preferred maximum and minimum zoom levels.
  6. hMap.resetMinMaxZoomPreference()

Figure 1 and Figure 2 show the effect before and after the camera position is specified.

Figure 1 Before specifying the camera position
Figure 2 After specifying the camera position

Area Constraints

The Map SDK allows you to set the map camera bounds. You can call the setLatLngBoundsForCameraTarget(LatLngBounds) API to specify a LatLngBounds object 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.

Search in Guides
Enter a keyword.