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
GuidesApplication ServicesMap KitMap InteractionMap Screenshot

Map Screenshot

This section describes how to implement the map screenshot function.

A map screenshot is a captured image of the visible region on a map, including map elements, overlays, and logos. The map screenshot function is applicable to scenarios where the current map state needs to be saved as an image, such as sharing the current location, generating a navigation route map, and recording map content from a specific perspective. This function helps you quickly implement visualized output of map content, improving user experience.

API Description

The following table describes the map screenshot API, which is provided by snapshot. For more information about the API, please refer to API Reference.

Expand
API Description
snapshot(): Promise<image.PixelMap> Takes map screenshots.

Development Procedure

  1. Import required modules.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { MapComponent, mapCommon, map } from '@kit.MapKit';
    2. import { AsyncCallback } from '@kit.BasicServicesKit';
    3. import { image } from '@kit.ImageKit';
  2. Call the snapshot method to take a screenshot of the current map screen.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. @Entry
    2. @Component
    3. struct HuaweiMapDemo {
    4. private mapOptions?: mapCommon.MapOptions;
    5. private callback?: AsyncCallback<map.MapComponentController>;
    6. private mapController?: map.MapComponentController;
    7. @State image?: image.PixelMap = undefined;
    8. aboutToAppear(): void {
    9. // Map initialization parameters used to set the coordinates of the map center point and zoom level.
    10. this.mapOptions = {
    11. position: {
    12. target: {
    13. latitude: 39.9,
    14. longitude: 116.4
    15. },
    16. zoom: 10
    17. }
    18. };
    19. // Map initialization callback.
    20. this.callback = async (err, mapController) => {
    21. if (!err) {
    22. // Obtain the controller class of the map to operate the map.
    23. this.mapController = mapController;
    24. } else {
    25. console.error(`Failed to initialize the map. Code: ${err.code}; message: ${err.message}`);
    26. }
    27. };
    28. }
    29. build() {
    30. Stack() {
    31. Column() {
    32. MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
    33. .width('100%')
    34. .height('50%');
    35. Scroll(new Scroller()) {
    36. Column() {
    37. Image(this.image)
    38. .objectFit(ImageFit.Auto)
    39. .border({ width: 1, color: Color.Red }).width("100%")
    40. Button("Get Screenshot")
    41. .margin({ left: 10 })
    42. .fontSize(12)
    43. .onClick(async () => {
    44. if (this.mapController) {
    45. // Obtain the screenshot.
    46. let pixelMap = await this.mapController.snapshot();
    47. this.image = pixelMap;
    48. }
    49. });
    50. }
    51. }.width('70%').height("50%")
    52. }.width('100%')
    53. }.height('100%')
    54. }
    55. }
Search in Guides
Enter a keyword.