目录

地图显示样式配置常见问题

Q:如何实现在地图上去掉地铁路线的显示?

A:地铁路线使用transit.metro-line属性控制,配置如下:

// 自定义样式参数
        let param: mapCommon.CustomMapStyleOptions = {
          styleContent: `[{\n            "mapFeature": "transit.metro-line",\n            "options": "all",\n            "visibility": false\n          }]`
        };

具体实现代码如下:

import { MapComponent, mapCommon, map } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
import { display } from '@kit.ArkUI';

@Entry
@Component
struct CustomMapStyleDemo {
  private mapOptions?: mapCommon.MapOptions;
  private mapController?: map.MapComponentController;
  private callback?: AsyncCallback<map.MapComponentController>;
  mapHeight: number = 0;

  aboutToAppear(): void {
    try {
      let displayClass = display.getDefaultDisplaySync();
      this.mapHeight = this.getUIContext().px2vp(displayClass.height);
    } catch (error) {
      console.error(`getDefaultDisplaySync error. Code is ${error.code}, message is ${error.message}.`);
    }

    // 地图初始化参数
    this.mapOptions = {
      position: {
        target: {
          latitude: 31.984410259206815,
          longitude: 118.76625379397866
        },
        zoom: 15
      }
    };
    this.callback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
        // 自定义样式参数
        let param: mapCommon.CustomMapStyleOptions = {
          styleContent: `[{\n            "mapFeature": "transit.metro-line",\n            "options": "all",\n            "visibility": false\n          }]`
        };
        // 设置自定义样式
        try {
          await this.mapController.setCustomMapStyle(param);
        } catch (error) {
          console.error(`setCustomMapStyle fail, code is:${error.code}, message is ${error.message}`);
        }
      } else {
        console.error(`init map fail, code is:${err.code}, message is ${err.message}`);
      }
    };
  }

  build() {
    Column() {
      MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
        .height(this.mapHeight)
        .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]);
    }
    .width('100%')
    .height('100%')
    .ignoreLayoutSafeArea();
  }
}

运行效果如下:

Q:地图缩放到全国的场景下,如何实现不显示各省份名称?

A:配置地图自定义JSON的administrative.province为不显示。代码示例如下:

import { map, mapCommon, MapComponent } from '@kit.MapKit';
import { AsyncCallback } from '@kit.BasicServicesKit';
import { display } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  private mapOptions?: mapCommon.MapOptions;
  private mapController?: map.MapComponentController;
  private callback?: AsyncCallback<map.MapComponentController>;
  @State mapHeight: number = 0;

  aboutToAppear(): void {
    let displayClass = display.getDefaultDisplaySync();
    this.mapHeight = this.getUIContext().px2vp(displayClass.height);
    // 地图初始化参数
    this.mapOptions = {
      position: {
        target: {
          latitude: 31.984410259206815,
          longitude: 118.76625379397866
        },
        zoom: 4
      }
    };
    this.callback = async (err, mapController) => {
      if (!err) {
        this.mapController = mapController;
        // 自定义样式参数
        let param: mapCommon.CustomMapStyleOptions = {
          styleContent: `[\n                            {\n                                "mapFeature": "administrative.province",\n                                "options": "all",\n                                "visibility": false\n                            }\n                        ]`
        };
        // 设置自定义样式
        await this.mapController.setCustomMapStyle(param);
      }
    };
  }

  build() {
    Stack() {
      Column() {
        MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
          .height(this.mapHeight);
      }.width('100%');
    }.height('100%')
    .ignoreLayoutSafeArea();
  }
}

实现效果: