
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
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
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);
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(), 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() {
let settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(settingsIntent);
}