0
点赞
收藏
分享

微信扫一扫

GPS定位_Autojs

sunflower821 2022-04-13 阅读 89
javascriptjs

在这里插入图片描述

/* 参考文章
https://blog.csdn.net/sxf1061700625/article/details/120597039
https://xfxuezhang.blog.csdn.net/article/details/107102858 */

importClass(android.content.Intent);
importClass(android.content.Context);
importClass(android.provider.Settings);
importClass(android.location.Location);
importClass(android.location.Criteria);
importClass(android.location.LocationManager);
importClass(android.location.LocationListener);

let location = getLocationLoop(),
    GPSInfo = getGPSInfo(location)
log(GPSInfo)

function getLocationLoop() {
    let locationManager = context.getSystemService(Context.LOCATION_SERVICE),
        criteria, provider, location
    /* 判断是否已经打开GPS模块 */
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        /* GPS模块打开,可以定位操作 */
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高
        criteria.setAltitudeRequired(true); //海拔信息:不需要
        criteria.setBearingRequired(true); //方位信息: 不需要
        criteria.setCostAllowed(true);  //是否允许付费
        criteria.setPowerRequirement(Criteria.POWER_LOW); //耗电量: 低功耗
        provider = locationManager.getBestProvider(criteria, true); //获取GPS信息
        if (provider != null) {
            locationManager.requestLocationUpdates(provider, 100, 5, new LocationListener({
                onLocationChanged(location) {
                    getGPAInfo(location)
                }
            }));
            location = locationManager.getLastKnownLocation(provider);
        } else {
            openGPS()
        }
        return location;
    }
}

function getGPSInfo(location) {
    if (location) {
        let gc = new android.location.Geocoder(context, java.util.Locale.getDefault()),
            result = gc.getFromLocation(location.getLatitude()/* 纬度 */, location.getLongitude()/* n经度 */, 1),
            GPSInfo = { Time: new Date(location.time) }
        result = result.get(0)
        Object.keys(result).filter(item => {
            return /^get/.test(item) && item != "getClass" && item != "getExtras"
        }).forEach(item => {
            let info = item != "getAddressLine" ? result[item]() :
                result.getAddressLine(0)
            info && (GPSInfo[item.replace("get", "")] = info)
        })
        return GPSInfo;
    }
}
function openGPS() {/* 打开gps */
    /* 定位服务 */
    let settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(settingsIntent);
    // app.startActivity({
    //     packageName: "com.android.settings",
    //     className: "com.android.settings.Settings$LocationSettingsActivity",
    //     data: "package:" + context.getPackageName().toString()
    // });
}
举报

相关推荐

0 条评论