Distance senstivitiy of onLocationChanged() - android

I am a newbie with java and android. I managed to follow some tutorials to get my gps to work on an android 2.3 phone, but i notice the phone's gps is only accurate within 40 meters, which correpsonds to an accuracy of the third decimal place in latitude/longitude.
My debug log shows this change(while im sitting on my laptop, typing)
LOCATION CHANGED(2137): 43.522701025009155
LOCATION CHANGED(2137): -72.13161981105804
LOCATION CHANGED(2137): 43.522695660591125
LOCATION CHANGED(2137): -72.13161981105804
This is my code
public void onLocationChanged(Location location) {
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(MainActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
else{
// Log.v(TAG,"LOCATION not found");
}
// Log.d("LOCATION is", location.getLatitude() + "");
}
Is it possible to change the onLocationChanged() function to something like
public void onLocationChanged(Location location, minimumdistance_changed)
where minimumdistance_changed is the accuracy of the current accuracy of gps. I'll also be happy with just setting the minimumdistance_changed value to 100meters as long as it will mean i only get a reading from onLocationChanged when the gps value changes to the third decimal place.

Upon requesting location updates from the location manager you can specify a minimum distance between updates:
lm.requestLocationUpdates(provider, timeBetweenUpdates, distBetweenUpdates, listener);
But is is only a hint, the system is not required to follow your hint.

Related

How to get current latitude and longitude and show in a toast?

Hello I know this question is already existed but not getting my solution till now . I have tried many methods to get current location in android studio using java but all are showing the same message location is null and latitude longitude are showing 0.0/0.0
Kindly if any body knows the perfect solution to fix this problem ?
You can use the LocationManager.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
Toast.makeText(this, "longitude " + longitude + "latitude " + latitude, Toast.LENGTH_SHORT).show();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
You can add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

LocationManager minDistance

My sample code:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 50.0f, this);
. . .
public void onLocationChanged(Location loc) {
System.out.println(loc.getLatitude()+ " - " + loc.getLongitude());
}
my minDistance parameter set to 50.0f (50 metres)
but onLocationChanged called many times even if I dont move phone at all.
As I found from docs:
The minDistance parameter can also be used to control the frequency of
location updates. If it is greater than 0 then the location provider
will only send your application an update when the location has
changed by at least minDistance meters.
so my question is why location changed ?

How do I switch providers after waiting for 'x' amount of time when attempting to retrieve a location fix?

I am building a GPS Android application whereby it retrieves the nearest places based on the user's current location. At first, I detect both GPS and network to see if they are enabled. If both are enabled I would use GPS first because it is the most accurate, and for my application it is safe to assume they are outside, therefore, retrieving GPS should not take too long. Nevertheless, there are always situations when GPS takes a long time. How do I therefore implement a way to switch over to NETWORK_PROVIDER if GPS takes over, for example, 2 minutes?
This is my code right now:
I check if GPS or internet is enabled.
if(!GPSEnabled && !networkEnabled)
{
Toast.makeText(this, "Error: This application requires a GPS or network connection",
Toast.LENGTH_SHORT).show();
}
else
{
if(GPSEnabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else if(networkEnabled)
{
System.out.println("Getting updates from network provider");
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}
This is the onLocationChanged method. I get the lat/lng values and then send them off to my server and then do appropriate stuff with it.
public void onLocationChanged(Location location)
{
//Get coordinates
double lat = (location.getLatitude());
double lng = (location.getLongitude());
Log.d("MainActivity", "got location: " + lat + ": " + lng);
//get nearest locations
new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng);
// Zoom in, animating the camera after the markers have been placed
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
System.out.println("lat = " + lat + ", lng = " + lng);
//Stop listening for updates. We only want to do this once.
locManager.removeUpdates(this);
}
What would I need to add to switch over to Network or GPS if either one takes too long?
I'd recommend to use both providers at the same time and determine more accurate location using, for example, isBetterLocation() function from this article: http://developer.android.com/guide/topics/location/strategies.html. In this case users won't have to wait 2 minutes to use your app, if GPS is slow. At first, you'll use network updates, and then, when GPS fixes are obtained, more accurate locations.

Android: Not able to get accurate latitude and longitude value

In my application I want latitude and longitude value by GPS and/ or Network. My code work but some time it give accurate value some time it not give the accurate value, some time it give the value which is 10 or 20 meter far from the accurate place.
Code:
mlocListener = new MyLocationListener(getApplicationContext());
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, 0, mlocListener);
} else if (mlocManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
mlocManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 10000, 0, mlocListener);
}
Location gpsLocation = mlocManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location netLocation = mlocManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gpsLocation != null) {
mlocListener.latitude = gpsLocation.getLatitude();
mlocListener.longitude = gpsLocation.getLongitude();
double lat = (double) (gpsLocation.getLatitude());
double lng = (double) (gpsLocation.getLongitude());
Toast.makeText(this, "Location attributes using GPS Provider",
Toast.LENGTH_LONG).show();
} else if (netLocation != null) {
mlocListener.latitude = netLocation.getLatitude();
mlocListener.longitude = netLocation.getLongitude();
double lat = (double) (netLocation.getLatitude());
double lng = (double) (netLocation.getLongitude());
Toast.makeText(this, "Location attributes using NETWORK Provider",
Toast.LENGTH_SHORT).show();
}
Use loc.getAccuracy() method to check the accuracy level of location you received. If the value is less then 10(or less than that) then you can consider it , otherwise wait for location Lister to fetch another location.
getLastKnownLocation is your last known location, dont use just getAccuracy also check the time.
Better dont use getLastKnownLocation if you need only accurate location.
Usually GPS is accurate upto 3m but if its 10 to 20 meters, it is something possible with GPS.
A standard GPS receiver for civil use offers an accuracy down to a few
meters. In praxis the number and geometry of the received satellites
influences the accuracy considerably, and in daily use, accuracies of
about 20 m can be expected.
GPS Accuracy
Also if you are getting your current location from network provider, you may expect even more inaccurate locations.
If you are looking forward to get maximum accuracy, I will say that you dismiss network provider because it is influenced by other factors related to phone operator etc.
locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
This get your location using your phone internal GPSreceiver (time costly) but it gives you the maximum possible accuracy.

Android Google Maps - getLastKnownLocation returns inaccurate latitude and longitude

I am developing an app where I need to calculate the distance from the current position and some other locations. I am using the GPS to access the users current location and the other locations coordinates are stored in a database. The problem occurs in the following snippet:
#Override
public void onLocationChanged(Location arg0) {
Log.v("LOCATION LAT", String.valueOf(arg0.getLatitude()));
currentLocation = arg0; //currentLocation is a global class variable
}
The problem is when I feed the DDMS with coordinates such as:
Latitude: 62.639579
Longitude: 17.909689 and log these values I get Latitude: 62.0 and Longitude 17.0 .
If I create a location object and set the lat and lng values myself it works. Like this:
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current.setLatitude(62.639579);
current.setLongitude(17.909689);
Log.v("Current LAT", "" + current.getLatitude());
}
EDIT SOLVED:
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently this should be delimited with a comma sign, not a dot...
Have you used the permissions specified in this post? Else it kicks back to using cell tower triangulation.
Other question
Found the problem. I was feeding the the DDMS with faulty formatting. Apparently the coordinates should be delimited with a comma sign, not a dot...
you can do something like as below. Create a location variable in that you have to assign location change var
#Override
public void onLocationChanged(Location arg0) {
Location current = new Location("Current location");
current=arg0;
Log.v("Current LAT", "" + current.getLatitude());
}

Categories

Resources