目录

Setting the Layering Order of Map Elements

Use Cases

This section describes how to set the layering order of map elements.

Set the display order of map elements from bottom to top, so that later elements will cover earlier ones.

Table 1 Layering order of map elements

Enumeration Enumerated Value Description
OVERLAY 1 Overlays, including MapCircle, MapPolygon, MapPolyline, MapArc, ImageOverlay, and TraceOverlay.
POI 2 Base map POI.
CUSTOM_POI 3 Overlays that support collision, including the PointAnnotation and Bubble.
MARKER 4 Markers, including the Marker and ClusterOverlay.

API Description

The function of setting the layering order of map elements is provided by MapElementType and setDisplayOrder. For more information about the APIs, please refer to API Reference.

API Description
mapCommon.MapElementType Map element type.
setDisplayOrder(types: Array<mapCommon.MapElementType>): void Sets the layering order of map elements.

Development Procedure

  1. Import required modules.

    import { mapCommon, map, MapComponent } from '@kit.MapKit';
    import { AsyncCallback } from '@kit.BasicServicesKit';
  2. Set the layering order of map elements.

    @Entry
    @Component
    struct MarkerDemo {
      private mapOptions?: mapCommon.MapOptions;
      private mapController?: map.MapComponentController;
      private callback?: AsyncCallback<map.MapComponentController>;
      private mapEventManager?: map.MapEventManager;
      private marker?: map.Marker;
      private bubble?: map.Bubble;
    
      aboutToAppear(): void {
        // Map initialization parameters.
        this.mapOptions = {
          position: {
            target: {
              latitude: 31.984410259206815,
              longitude: 118.26625379397866
            },
            zoom: 10
          }
        };
        this.callback = async (err, mapController) => {
          if (!err) {
            this.mapController = mapController;
            this.mapEventManager = this.mapController.getEventManager();
            // Marker initialization parameters.
            let markerOptions: mapCommon.MarkerOptions = {
              position: {
                latitude: 31.984410259206815,
                longitude: 118.26625379397866
              },
              rotation: 0,
              visible: true,
              zIndex: 0,
              alpha: 1,
              anchorU: 0.5,
              anchorV: 1,
              clickable: true,
              draggable: true,
              flat: false
            };
            // Create a Marker.
            try {
              this.marker = await this.mapController.addMarker(markerOptions);
            } catch (e) {
              console.error(`Failed to create the marker. Code: ${e.code}; message: ${e.message}`);
            }
            let bubbleOptions: mapCommon.BubbleParams = {
              // Bubble position.
              positions: [[{
                latitude: 32.384410259206815,
                longitude: 118.26625379397866
              }]],
              // Set icons. Icons for four directions must be provided and stored in resources/rawfile.
              icons: [
                'speed_limit_10.png',
                'speed_limit_20.png',
                'speed_limit_30.png',
                'speed_limit_40.png'
              ],
              // Set the display attribute of the Bubble. If it is set to true, the Bubble remains visible after collision.
              forceVisible: true,
              // Define the Bubble collision priority. A larger value indicates a lower priority.
              priority: 3,
              // Define the minimum zoom level for Bubble display.
              minZoom: 2,
              // Define the maximum zoom level for Bubble display.
              maxZoom: 20,
              // Define whether the Bubble is visible.
              visible: true,
              // Define the overlay level attribute of the Bubble.
              zIndex: 1
            }
            // Add a Bubble.
            try {
              this.bubble = await this.mapController.addBubble(bubbleOptions);
            } catch (e) {
              console.error(`Failed to create the bubble. Code: ${e.code}; message: ${e.message}`);
            }
            let imageOverlayParams: mapCommon.ImageOverlayParams = {
              // Range of the ImageOverlay.
              bounds: {
                southwest: {
                  latitude: 32,
                  longitude: 118
                },
                northeast: {
                  latitude: 32.4,
                  longitude: 118.4
                }
              },
              // Image of the ImageOverlay.
              image: 'icon/icon.png',
              transparency: 0.3,
              zIndex: 101,
              anchorU: 0.5,
              anchorV: 0.5,
              clickable: true,
              visible: true,
              bearing: 0
            };
            // Add an ImageOverlay.
            try {
              await this.mapController?.addImageOverlay(imageOverlayParams);
            } catch (e) {
              console.error(`Failed to create the imageOverlay. Code: ${e.code}; message: ${e.message}`);
            }
    
            // Set the elements in the following order: overlays at the bottom layer, followed by POIs, overlays that support collision, and markers at the topmost.
            let mapElementTypeArr: Array<mapCommon.MapElementType> = [
              mapCommon.MapElementType.OVERLAY,
              mapCommon.MapElementType.POI,
              mapCommon.MapElementType.CUSTOM_POI,
              mapCommon.MapElementType.MARKER];
            this.mapController.setDisplayOrder(mapElementTypeArr);
          } else {
            console.error(`Failed to initialize the map. Code: ${err.code}; message: ${err.message}`);
          }
        }
      }
    
      build() {
        Stack() {
          Column() {
            MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback });
          }.width('100%')
        }.height('100%')
      }
    }