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

    接口 YmxTencentLocation

    腾讯位置服务定位

    基于腾讯位置服务定位 SDK,提供单次定位与连续定位能力。

    • 使用前需在腾讯位置服务控制台创建应用, 获取 API Key 复制到本插件的打包配置
    • 首次定位时会自动请求定位权限,请允许授权
    • 连续定位结果通过 setListener 异步回传,eventName 为 location / error,见 EventData
    const qqLoc = ymx.requirePlugin('ymx-tencent-location') as YmxTencentLocation;

    // 单次定位
    const res = await qqLoc.getCurrentPosition();
    console.log('经度:', res.longitude, '纬度:', res.latitude);

    // 连续定位
    qqLoc.setListener((eventName, data) => {
    if (eventName === 'location') {
    console.log('实时位置:', data.data.longitude, data.data.latitude);
    }
    });
    qqLoc.getCurrentPosition({
    watch: true,
    watchInterval: 3
    });

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

    层级

    • JsApiBase
      • YmxTencentLocation
    索引

    属性

    id: string

    获取插件 ID

    version: string

    获取插件版本,如 1.2.3

    方法

    • 获取当前位置

      支持单次定位与连续定位:

      • 单次定位(watch 为 false):定位一次后自动停止,通过返回值获取结果
      • 连续定位(watch 为 true):按 watchInterval 间隔持续定位,结果通过 setListenerlocation 事件回传,调用 stop 停止

      参数

      返回 Promise<LocationResult>

      定位结果(单次定位)

      // 单次定位
      const res = await qqLoc.getCurrentPosition();
      console.log('经度:', res.longitude, '纬度:', res.latitude);

      // 连续定位
      qqLoc.setListener((eventName, data) => {
      if (eventName === 'location') {
      console.log('实时位置:', data.data.longitude, data.data.latitude);
      }
      });
      await qqLoc.getCurrentPosition({
      watch: true,
      watchInterval: 3,
      notAddress: true
      });

      // 停止定位
      await qqLoc.stop();
    • 移除监听器

      返回 Promise<void>

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

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

      参数

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

      返回 Promise<void>

    • 停止定位

      停止连续定位,单次定位无需调用。

      返回 Promise<void>

      await qqLoc.stop();