问题
in out app we have a module where you can send time records with GPS location.
In 40% of cases we have location problem because the module provides a location which is not real (sometime about 1 km away).
When we have this cases we open Google Maps app and it give us perfect location, then again out app and not-real location.
For getting the location we use this:
Slider s1 = new Slider();
Dialog dlg = makeDialog("HIGH PRECISION LOCATION...", s1, null, 'a');
dlg.addShowListener((ActionListener) e -> {
LocationManager locationManager = LocationManager.getLocationManager();
locationManager.setLocationListener(this, new LocationRequest(LocationRequest.PRIORITY_HIGH_ACCUARCY, 500));
loc = locationManager.getCurrentLocationSync(20000);
dlg.dispose();
if(loc == null) {
Slider s2 = new Slider();
Dialog dlg2 = makeDialog("GETTING LAST KNOWN LOCATION...", s2, "OK", 'a');
dlg2.addShowListener((ActionListener) e2 -> {
loc = locationManager.getLastKnownLocation();
if(loc == null) {
// location not found
Dialog.show("Attenzione!", "Posizione non trovata. E' consigliato di spostarsi all'aperto. "
+ "Tuttavia è possibile inviare la timbratura anche senza coordinate.", "Ok", null);
} else {
paintLocation(loc, GPSStateOn);
}
});
dlg2.show();
} else {
paintLocation(loc, GPSStateOn);
}
});
dlg.show();
-
@Override
public void locationUpdated(final Location location) {
switch (LocationManager.getLocationManager().getStatus()) {
case LocationManager.AVAILABLE:
GPSStateOn = true;
break;
case LocationManager.OUT_OF_SERVICE:
GPSStateOn = false;
break;
case LocationManager.TEMPORARILY_UNAVAILABLE:
GPSStateOn = false;
break;
}
if(loc != null) {
paintLocation(loc,GPSStateOn);
System.out.println("-----LATITUDINE: " + loc.getLatitude());
System.out.println("-----LONGITUDINE: " + loc.getLongitude());
}
}
We also have added the buil hint android.playService.location = true
ADDED 25/04/2020 11:58 I forgot to say that about 40% of cases it arrives at "GETTING LAST KNOWN LOCATION..." and then give perfect real location. Android build hints:
last one is cutted... value is:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/><uses-feature android:glEsVersion="0x00020000" android:required="true"/><uses-permission android:name="android.permission.CAMERA"/><uses-feature android:name="android.hardware.camera" android:required="false"/>
ADDED 06/05/2020 10:48
Maybe this can be useful: I noticed when the location takes a lot the GPS icon isn't displayed in the topbar
And when it takes less time the GPS icon is visible
回答1:
Looking again at the code I noticed you used set location listener and getCurrentLocationSync
which are mutually exclusive in this case. You need to pick one. If you give the former (which is the preferred way) enough time to work it should give you an accurate location. Notice the GPS position sometimes needs a couple of seconds for the initial reading.
来源:https://stackoverflow.com/questions/61403935/codename-one-app-not-provide-real-location