How to get the satellite count from the fused location api? - android

I am using GoogleApiClient to request location update. I am getting the latitude, longitude, speed and other information. Along with this I want to get the satellite count also. How can I achieve this?
Please help me on this.

Here is the sample code to get the satellite count in use and in view
gpsStatus = locationManager.getGpsStatus (gpsStatus);
Iterable<GpsSatellite> satellites = gpsStatus.getSatellites ();
int sattInView = 0;
int sattInUse = 0;
if (satellites != null) {
for (GpsSatellite gpsSatellite : satellites) {
sattInView++;
if (gpsSatellite.usedInFix ()) {
sattInUse++;
}
}
}
inView = String.valueOf (sattInView);
inUse = String.valueOf (sattInUse);

Related

Android MapsApiV2 Location.distanceTo is giving wrong values

This seems like a duplicate question but it is not. Please read before you mark it as duplicate
I was trying to get distance between two LatLng, so first i generated it's PolylineOptions,
Now the problem is - The output is not understandable.
It's different from other questions because it uses PolylineOptions.
Calculating Code
#Override
public void onParserTaskComplete(PolylineOptions points, int count) {
if (points != null) {
float totalDistance = 0;
for (int i = 1; i < points.getPoints().size(); i++) {
Location currLocation = new Location("this");
currLocation.setLatitude(points.getPoints().get(i).latitude);
currLocation.setLongitude(points.getPoints().get(i).longitude);
Location lastLocation = new Location("this");
currLocation.setLatitude(points.getPoints().get(i - 1).latitude);
currLocation.setLongitude(points.getPoints().get(i - 1).longitude);
totalDistance += lastLocation.distanceTo(currLocation);
}
items.get(count).setDistance(String.valueOf(totalDistance));
adapter.notifyDataSetChanged();
} else {
items.get(count).setDistance("");
adapter.notifyDataSetChanged();
}
}
According to Google Maps the distance should be around 5.5KM
but output is coming like 8783711.0
Question - How do i convert this value to KM?
You duplicated currLocation and don't set values for lastLocation.
This is correct way:
Location currLocation = new Location("this");
currLocation.setLatitude(points.getPoints().get(i).latitude);
currLocation.setLongitude(points.getPoints().get(i).longitude);
Location lastLocation = new Location("this");
// here you used currLocation
lastLocation.setLatitude(points.getPoints().get(i - 1).latitude);
lastLocation.setLongitude(points.getPoints().get(i - 1).longitude);

Chat Application with Maps

I want to build a basic chat application which includes location sharing. I proficient enough in Java, but haven't worked enough on Android Platform. How do I approach this problem and what are the steps to be taken?
Send location values latitude and longitude to the other person via chat message. You can get user's last known location by:
private double[] getGPS()
{
LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop */
Location l = null;
for (int i = providers.size() - 1; i >= 0; i--)
{
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
double[] gps = new double[2];
if (l != null)
{
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}

NullPointerException Sometimes

I have a function in my application and when user clicks button, it will display the nearest office. Functionality works in part of Sweden but does not work in the north of Sweden. I use try-catch statement and I'll catch the null pointer exception. When I shut down my network and the location, I get the error message, but in north Sweden, there is no message, the app just crashes. My code is down there. onFindOfficeClick () is a single function and independent. How can I solve this problem? I tried debug but fails to hit the error ... do not know what to do. I take all coordinates from xml file. Does anybaody have any suggestions?
private void onFindOfficeClick() {
try {
LocationManager lm = (LocationManager)getSystemService(Service.LOCATION_SERVICE);
List<String> providersName = lm.getProviders(false);
if(providersName.size()>0){
Location location = lm.getLastKnownLocation(providersName.get(0));
float results [] = new float [1];
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
final int officeCount = mOfficesInfo.length;
double minDistance = 0;
int officeNum = 0;
for(int i=0; i<officeCount; i++){
Location.distanceBetween(latitude, longitude,
mOfficesInfo[i].getLatitude(), mOfficesInfo[i].getLongitude(), results);
if(i==0){
minDistance = results[0];
} else if(results[0]<minDistance){
minDistance=results[0];
officeNum = i;
}
}
int countryPosition = mCountries.indexOf(mOfficesInfo[officeNum].getCountry());
mCountrySpiner.setSelection(countryPosition);
mFindedOfficeName = mOfficesInfo[officeNum].getOfficeName();
mOfficesSpiner.setSelection(officeNum);
}
}
catch (NullPointerException e)
{
Toast.makeText(getApplicationContext(),"check your network and GP:s connections",Toast.LENGTH_SHORT).show();
}
}
put a null check before calling
LocationManager lm = (LocationManager)getSystemService(Service.LOCATION_SERVICE);
if(lm!=null){
lm.getProviders(false);.
}
and also might be
Location location = lm.getLastKnownLocation(providersName.get(0));
giving you null in the location variable so just do modify your code and put null check before getting latitude and laongitude from location.
if(location!=null){
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
}
becuase it is not good practice to catch NullPointerException in you code.

How to get the latitude and longitude or gps data quickly while we are indoor in an android phone?

I'm trying to get the latitude and longitude information from an android phone through GPS, when i'm outdoor or under the sky directly i'm able to get the values instantly but when i'm indoor or inside a room its taking more than a minute to get the values. Can anyone help me in getting this values fastly when I'm using my app inside a room.
I'm using the following code in getting the values:
LocationManager locManager;
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
EditText myLocationText = (EditText)findViewById(R.id.editText1);
EditText myLocationText1 = (EditText)findViewById(R.id.editText2);
String latString = "";
String LongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latString = "" + lat;
LongString ="" + lng;
} else {
latString = "No location found";
LongString = "No location found";
}
myLocationText.setText(""+ latString);
myLocationText1.setText(""+ LongString);
}
Is there any other way in getting the GPS values other than using LocationManager??
You can get the last known location, and it's quite fast:
/**
* Gets the last known location.
*
* #param locationManager the location manager
* #return the last known location
*/
public static Location getLastKnownLocation(LocationManager locationManager)
{
Location bestResult = null;
float bestAccuracy = 10000;
long bestTime = 0;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Log.d("LOCATION", "Provider: " + provider);
Location location = locationManager.getLastKnownLocation(provider);
Log.d("LOCATION", "Location found? "+ (location==null?"NO":"YES"));
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
Log.d("LOCATION", "Acc: "+ String.valueOf(accuracy) + " -- Time: " + String.valueOf(time));
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime &&
bestAccuracy == Float.MAX_VALUE && time > bestTime){
bestResult = location;
bestTime = time;
}
}
}
Log.d("LOCATION", "BEST FOUND? "+ (bestResult==null?"NO":"YES"));
return bestResult;
}
You can get the last known location of the device using the following method in LocationManager, this is the location that has been last found system-wide.
This method is the key:
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
See for more details Obtaining User Location
If you are indoors, you would be better of using LocationManager.NETWORK_PROVIDER as you are unlikely to get a signal inside. You could also use getLastKnownLocation(), but this may be non existent or out of date.
If you want get position in a room, gps provider is slowly. You can try changing:
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
locationListener);
Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
By:
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000L,500.0f,
locationListener);
And you will receive location in 'public void onLocationChanged(Location location)' method.
I used Network provider to fetch the Co-Ordinates and it is always much faster inside the house in living room. But when I try the same in the Basement area, it doesn't fetch the coordinates at all though I waited almost couple of minutes.
Note: I could able to make a call from basement area and I am also able to browse as well..

Implementing Kalman filter with Fusion Provider in Android for GPS positions

I have to implement Kalman filter for a better accuracy with GPS positions... I use Stochastically solution (Smooth GPS data). In ValidPosition I have some checks like:
public boolean checkPosition(Location location) {
if (( location != null ) &&
(distance != 0) &&
(distance > MINIMUM_DISTANCE) && // 10 metres
(location.hasSpeed()) &&
(location.getSpeed() > 0) &&
(averageTime < HUMANSPEED) &&
(location.hasAccuracy()) &&
(location.getAccuracy() < MINIMUM_ACCURACY) &&
(isBetterLocation(location, lastLocation)) // From Google example in http://developer.android.com/guide/topics/location/strategies.html#BestPerformance
return true;
}
return false;
}
Now, in my main class with Location Fusion Provider I have this:
public static final int ACCURACY_DECAYS_TIME = 3; // Metres per second
private KalmanLatLong kalmanLatLong = new KalmanLatLong(ACCURACY_DECAYS_TIME);
private ValidPosition validPosition = new ValidPosition();
#Override
public void onLocationChanged(Location location) {
if(validPosition.checkPosition(location)) {
kalmanLatLong.process(
location.getLatitude(),
location.getLongitude(),
location.getAccuracy(),
location.getTime());
mCallback.handleNewLocation(location);
}
}`
And now? How can I use Kalman predictions? What do I have write here in Stochastically code?
// TODO: USE VELOCITY INFORMATION HERE TO GET A BETTER ESTIMATE OF CURRENT POSITION
Thanks
You forgot to get processed result back from the kalmanLatLong object.
You need to do something like this:
if(validPosition.checkPosition(location)) {
kalmanLatLong.process(
location.getLatitude(),
location.getLongitude(),
location.getAccuracy(),
location.getTime());
location.setLatitude(kalmanLatLong.get_lat());
location.setLongitude(kalmanLatLong.get_lng());
location.setAccuracy(kalmanLatLong.get_accuracy());
mCallback.handleNewLocation(location);
}
As for your second question, I don't think it's trivial and should be based more on experimental data. Simple assumption with linear decay works well in most cases.

Categories

Resources