ymx-amap-location
    正在准备搜索索引...

    接口 YmxAmapLocation

    高德定位服务

    // 获取插件实例
    const amapLoc = ymx.requirePlugin('ymx-amap-location') as YmxAmapLocation;

    // 单次定位
    const result = await amapLoc.getCurrentPosition({
    timeout: 10,
    locationMode: 1
    });
    console.log(`经度: ${result.longitude}, 纬度: ${result.latitude}`);

    // 监听连续定位结果
    amapLoc.setListener((eventName, res) => {
    if (eventName === 'location') {
    console.log('实时位置:', res);
    }
    });
    // 开启连续定位
    await amapLoc.getCurrentPosition({
    watch: true,
    watchInterval: 3
    });

    // 停止定位
    await amapLoc.stop();
    interface YmxAmapLocation {
        id: string;
        version: string;
        getCurrentPosition(option?: LocationOption): Promise<LocationResult>;
        removeListener(): Promise<void>;
        setListener(
            listener: (eventName: string, data: any) => void,
        ): Promise<void>;
        stop(): Promise<void>;
    }

    层级

    • JsApiBase
      • YmxAmapLocation
    索引

    属性

    id: string

    获取插件 ID

    version: string

    获取插件版本,如 1.2.3

    方法

    • 获取当前位置

      参数

      返回 Promise<LocationResult>

      const amapLoc = ymx.requirePlugin('ymx-amap-location') as YmxAmapLocation;

      // 单次定位
      amapLoc.getCurrentPosition({
      timeout: 10,
      locationMode: 1
      }).then(res => {
      console.log(`经度: ${res.longitude}, 纬度: ${res.latitude}`);
      }).catch(err => {
      console.error(err);
      });

      // 监听连续定位结果
      amapLoc.setListener((eventName, res) => {
      if (eventName === 'location') {
      console.log('实时位置:', res);
      }
      });
      // 开启连续定位
      amapLoc.getCurrentPosition({
      watch: true,
      watchInterval: 3
      });
    • 移除监听器

      返回 Promise<void>

    • 设置监听器,接收 eventName 事件名称,data 事件数据

      注意: 此接口为覆盖操作,仅保留最后一个监听器,最后的 listener 才能收到事件通知。

      参数

      • listener: (eventName: string, data: any) => void

      返回 Promise<void>

    • 停止定位

      返回 Promise<void>

      const amapLoc = ymx.requirePlugin('ymx-amap-location') as YmxAmapLocation;
      await amapLoc.stop();