前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Autojs获取GPS定位信息

Autojs获取GPS定位信息

作者头像
小锋学长生活大爆炸
发布2021-10-08 10:19:32
2.7K0
发布2021-10-08 10:19:32
举报

记得先把autojs的“定位权限”给开了!!!!

代码语言:javascript
复制
console.show();
importClass(android.content.BroadcastReceiver);
importClass(android.content.Intent);
importClass(android.content.Context);
importClass(android.app.PendingIntent);
importClass(android.provider.Settings);
importClass(android.net.Uri);
importClass(android.content.IntentFilter);
importClass(android.location.LocationManager);
importClass(android.location.Location);
importClass(android.location.LocationListener);
importClass(android.location.Criteria);

    
function getLocation(){
    var mLocationManager =context.getSystemService(Context.LOCATION_SERVICE);
    var criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高
    criteria.setAltitudeRequired(true); //海拔信息:不需要
    criteria.setBearingRequired(true); //方位信息: 不需要
    criteria.setCostAllowed(true);  //是否允许付费
    criteria.setPowerRequirement(Criteria.POWER_LOW); //耗电量: 低功耗
    
    var provider =mLocationManager.getBestProvider(criteria, true); //获取GPS信息
    log(provider);
    var location = mLocationManager.getLastKnownLocation(provider);
    mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener);
    return location;
}

function openGPS(){
    var settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(settingsIntent);
}//打开gps

function gpsIsOpen(){
    var alm=context.getSystemService(Context.LOCATION_SERVICE);     
    var bRet=true;
    if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
    bRet = false;
    }
    return bRet;
}//判断gps是否打开


if(!gpsIsOpen()){openGPS();}
locationListener=new LocationListener(){
 onLocationChanged(location){
  if(location!=null){
   log("维度:"+location.getLatitude()+"\n经度:"+location.getLongitude());
  }else{
   log("获取不到数据");
  }
 }
};

var mLocation = getLocation();
console.clear();
log(Object.keys(mLocation));
log("位置信息:"+mLocation);
log("gps时间:"+new Date(mLocation.time));
log(mLocation);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-10-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档