aardio 文档

aardio 范例: 调用 UWP / 定位

相关范例 - 地理定转归属地

import console;
import dotNet.uwpCompiler;

// 创建 UWP 编译器
var uwp = dotNet.uwpCompiler( "\aardio.geolocation.dll" );
uwp.Parameters.CompilerOptions = "/optimize";

// 编写 C# 源码封装 UWP 调用逻辑
uwp.Source = /****** 
using System;
using System.Collections;
using System.Threading.Tasks;
using Windows.Devices.Geolocation; // 引用 UWP 定位命名空间

namespace aardio.WinRT {
    public class GeoHelper {

        public async Task<Hashtable> GetLocationAsync() {
            try {
                // 请求定位权限(必须步骤,否则可能静默失败)
                var accessStatus = await Geolocator.RequestAccessAsync();
                if (accessStatus != GeolocationAccessStatus.Allowed) {
                    return new Hashtable { { "error", "Access Denied: " + accessStatus.ToString() } };
                }

                // 初始化定位器
                var locator = new Geolocator();
                // 设置高精度 (PositionAccuracy.High)
                locator.DesiredAccuracy = PositionAccuracy.High; 

                // 获取位置 (参数1: 最大老化时间, 参数2: 超时时间 10秒)
                // 如果不设置超时,在无法获取 GPS 信号时可能会一直挂起
                var pos = await locator.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(10));

                var coord = pos.Coordinate.Point.Position;

                // 返回 Hashtable,aardio 会自动将其转换为 table
                return new Hashtable {
                    { "latitude", coord.Latitude },
                    { "longitude", coord.Longitude },
                    { "altitude", coord.Altitude },
                    { "accuracy", pos.Coordinate.Accuracy },
                    { "source", "Windows Location Service" }
                };
            }
            catch (Exception ex) {
                return new Hashtable { { "error", ex.Message } };
            }
        }
    }
}
******/

// 编译 C# 代码
var assembly = uwp.Compile("~/lib/dotNet/geolocation/.dll/Geolocation.dll")

// 加载类并创建实例
var geoHelper = assembly.new("aardio.WinRT.GeoHelper");

console.showLoading("正在获取定位信息...");

// 调用异步方法。UWP 接口大多是异步的,返回 Task 对象
var task = geoHelper.GetLocationAsync();

// 同步等待 Task 完成并获取结果,在等待时保持界面响应
var geolocation = dotNet.wait(task);

// 直接访问 .NET HashTable 对象的键值
console.log( geolocation["latitude"] )

//上面的 DLL 已经封装为了 dotNet.geolocation 扩展库
import dotNet.geolocation;

//获取地址位置
var geolocation,err = dotNet.geolocation.get();

if(err){
    console.error("定位失败"); 
    raw.execute("ms-settings:privacy-location"); // 打开系统定位设置
    return; 
}

// 遍历 .NET HashTable 对象
for i,dictEntry in dotNet.each(geolocation) {   
    console.log(dictEntry.Key,dictEntry.Value)
}

// 直接访问 .NET HashTable 对象的键值
console.log( "altitude",geolocation["altitude"] )
console.log( "latitude",geolocation["latitude"] )

// GPS 定位精度为 3-10 米,Wi-Fi 或基站定位精度通常为数十到数百米
console.log( "accuracy",geolocation["accuracy"] )

// 7.  转换为纯 aardio 表对象
var geolocation = table.parseValue(geolocation)
console.dumpJson(geolocation)

//相关范例 - 地理定转归属地: https://www.aardio.com/zh-cn/doc/example/Web/REST/geolocation.html

console.pause();
Markdown 格式