Android GPS bearing and speed always zero - android

I am testing my app on emulator with a gpx file.
Coordinates change, but bearing and speed are always zero.
I've checked my LocationManager settings, and both .supportsSpeed() and .supportsBearing() return true.
My GPS code is almost one-to-one with this one http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/ but this part is most important so I paste it here:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsBearing()).toString());
Log.d("GPS support",((Boolean)locationManager.getProvider(locationManager.GPS_PROVIDER).supportsSpeed()).toString());
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
I've only added this code
#Override
public void onLocationChanged(Location location) {
this.location = location;
}
I instantiate GPSTracker object in activity's onCreate method, and then use gps.getLocation() where needed.
My .gpx file used for testing is available here: http://snk.to/f-cdzj45ic

Tried it on a physical device, and worked well.
I just thought that gpx with list of waypoints is enough, and bearing and speed will be calculated from it.

Related

How to get current location in android 7?

I'm trying to get current location in android and its OK when i'm using on android M or lower, But when i run my application on android N, location is null and when i want get latitude from it will returns 0. here is my code
try {
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
and its OK when i'm using on android M or lower
No, it is not. getLastKnownLocation() frequently returns null, on all versions of Android.
getLastKnownLocation() is an optimization. Use it, but be prepared to rely upon onLocationChanged() for when getLastKnownLocation() returns null or an out-of-date value.
This is covered in the documentation, as well as in books and courses on Android app development.
To get the last known location all your have to do is call
mLocationClient.getLastLocation();
once the location service was connected.
read how to use the new location API
http://developer.android.com/training/location/retrieve-current.html#GetLocation

Error when retrieving latitude and longitude

here's my problem :
I tried this tutorial to have the longitude and latitude of the device :
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
It worked fine on my phone (API 17) but didn't work on an emulated phone (API 25) nor a emulated TV (API 24). I always get latitude 0.0 and longitude 0.0
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
//if internet and gps doesn't work
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
After some test, I found that here :
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
The code go in the catch.
Does anyone know why it works on my phone and not in the android emulated devices ?
I found the answer : I tried my code on an AVD API 22 and it works just fine so this code is good until API 23, then you must use another one.
If I find a working code I'll post it.
EDIT
The code is good it's just that in api >=23 you have to ask the user if you're using "dangerous permission"

GPS ACCURACY IN ALL SCENARIOS ANDROID

I am working on a project which is GPS related. I have used both GPS and Network Provider to fetch the coordinates of current location.But the problem I am facing is Sometimes,It couldn't fetch the accurate location.
Also,when I am inside the building or closed roof my application couldn't fetch the accurate GPS location.
So,if anyone could help me by suggesting the best way of dealing with the location manager and the providers,it would be very helpful for me.
Please find the code below:
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
GPS is only accurate with a good clear view of the sky, as the signals are of a wavelength that don't penetrate buildings easily. This means that if you're between tall buildings, you may get an inaccurate position (canyon effect), and if you're inside, you may not get a GPS fix at all. Even if you are outside, with a good view of the sky, the accuracy of a GPS fix is much less than the resolution.
This is not something that can be overcome with code, as it is down to physical limits.

How can i get last location without enabling location service on device?

I want to get last location (With any way) without enabling location services on device , is this possible?
How should i do this?
there are three ways to find your location,refer this
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
int cellid= cellLocation.getCid();
int celllac = cellLocation.getLac();
If you want to get location without GPS then you should use NETWORK_PROVIDER to get location :
I have taken this module from AndroidHive website You can refer more there.
Your query will be solved with this method getLocation()
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
You can use NETWORK_PROVIDER as service parameter in the method getLastKnownLocation.

Altitude getting zero always through gps android

hello,this is my code that I am used to get latitude,longitude and altitude.Longitude latitude getting properly but altitude return always zero.I could not understand how to get altitude.please check code and let me know.
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if(!location.hasAltitude())
{
altitude=location.getAltitude();
}else
{
altitude=1.0;
}
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if(!location.hasAltitude())
{
altitude=location.getAltitude();
}else
{
altitude=1.0;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
You're calling getAltitude() only when hasAltitude() returns false (when no altitude data is available and therefore 0.0 is returned).
that depends of the gps receiver installed on your phone. If the vendor implements some sort on not standard nmea implementation, is possible that not all the information are available. You can check if the chipset does support altitude through the sdk. Here the documentation

Categories

Resources