Android get Location doesnt work properly - android

Im trying to get location and the code works but it does not work properly. Here is my code
// Get the location manager
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
try {
lat = (int)Math.round((location.getLatitude () * 1e6) ) ;
lon = (int)Math.round((location.getLongitude() * 1e6) ) ;
}
catch (NullPointerException e){
lat = -1;
lon = -1;
}
the problem is when Im using foursqure or facebook they get the location directly but my application returns -1,-1 (sometimes) and sometimes it finds the location accurately. What might be the problem or how can I improve to get the current location better ?? Thanks

You should implements a LocationListerner and request location update. Once you get a fix you can remove the location update. You can read more info at http://developer.android.com/reference/android/location/LocationListener.html

The call to getLastKnownLocation sometimes returns null, as specified in the Andorid documentation. So sometimes that code will indeed produce lat=lon=-1. Also, getBestProvider is a way of determining the provider that best satisfies given criteria. Since you're not specifying any criteria, the logical conclusion is that all providers are equally good, so the result may be random. Finally, note that lat=-1 and lon=-1 is a valid location, so in general it's not a good way of flagging an error.
As suggested in another answer here, you should implement http://developer.android.com/reference/android/location/LocationListener.html . You then have the problem of which providers to use. My approach is to use them all and use a simple Kalman Filter to take care of the fact that different providers at different times have different levels of accuracy. I posted the simple kalman filter that I use for this here Smooth GPS data

Related

Android: How to move a GPS Location object closer to another Location object

I'm currently doing some research about a very small app I wanna try building.
The core concept will consist of moving randomly spawned GPS Location objects closer to the Location of the user as time passes by.
I have been looking through the Location API and as I understand all I need to do is updating the altitude and latitude of the spawned Location object to change its position. Now I'm curious what the best method is to: "Move a Location closer to another Location by 10 meter".
Is there already some existing API that achieves that goal like this hypothetical function:
Location newLocation = Location.moveTo(Location locationFrom, Location locationTo, int distance);
or do I have to write my custom algorithm?
What would be an efficient approach in this case? Any inputs are highly appreciated!
If the problem would exist in a Cartesian coordinate system I would just substract the user Location from the spawned Location to get the direction I need to move too and then add parts of the new vector to the spawned Location to get it closer.
How can this get solved with Location's ?
EDIT: I sumbled upon this thread: Move a LatLng 5 meters towards another LatLng
I guess using SphericalUtil.java is the way to do it.

Why I am getting different GPS point at same location

I am developing an android application to track user movement over a Google map. I am using GPS to pull the user location. It works perfectly fine when I move over road and change my location continuously.
The issue is, when I stop and remain at a point (stops my movement) the GPS keep sending different GPS coordinates.
Please note that I am using GPS in open space and I have used -
Here is the location manager details I am using -
LocationManager locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
criteria.setSpeedAccuracy(Criteria.ACCURACY_HIGH);
String provider = locManager.getBestProvider(criteria, true);
locManager.requestLocationUpdates(provider,0,0, this);
Please let me know, if I am not using it properly.
I have also gone through various blogs and tutorials and learnt that GPS is not perfect at a single location. In that case how I will handle this situation. My aim is to get a single GPS while I am not moving and standing on a single position.
Please let me know.
Thank you !
RequestLocationUpdates method uses a float value for the third parameter, if you leave it at 0 it will continue to update even if you move a step or two. If you set it to a different number like 50.0 it will update every 50 metres.

How to avoid frequent location updates?

I am new to android. I want to get GPS locations at an interval of 3 minutes, but I'm getting location updates on every second.
How can I avoid this?
Please suggest something. I'm using the following code:
captureFrequencey=3*60*1000;
LocationMngr.requestLocationUpdates(
LocationManager.GPS_PROVIDER, captureFrequencey, 0, this);
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 0, this);
Thats because you set the minimum distance to 0. Change it to 100 meters and check!
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, captureFrequencey, 100, this);
Refer to the documentation.
Lazy Ninja has already given you the solution.
But you should understand that, when you call request location updates it checks for conditions min time & min dist and if anyone is true it catches the location. In your code
LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
captureFrequencey, 0, this);
you have mentioned 0 as min distance, this condition is true all the time. So you are getting frequent location updates.
Hope this info helps u.
See the Location Strategies in docs
Just a quick point to add to the correct answers provided... setting the update time & distance will not work exactly the same on all devices.
These parameters are treated as "hints" to the Location Provider.
So while it is good practice, some devices may not always obey these exact values.

Android gps how to skip distance?

I am working on a GPS-enabled application and I need to record a point each N meters. However, I can't see how I can use onLocationChanged() method in the LocationListener or any other method/class. The onLocationChanged() method gives a point each second, and I need to store each N-meter point.
I believe that this has a simple solution, but since I am beginner in Android, cant find it.
Any help will be much appreciated.
requestLocationUpdates has a minDistance parameter, that
if I recall correctly does what you want. I haven't been able to test this on a real phone though, so I don't know how accurate it is.
In onLocationChanged, compare the location you get with the last one you stored. If it's less than n meters, discard it. If not, store it. Rinse. Repeat.
EDIT: Wait, even easier - doesn't requestLocationUpdates have a minDistance parameter? See here: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates
it is working perfectly
myManager = ((LocationManager) ApplicationController.getAppContext().getSystemService( Context.LOCATION_SERVICE ));
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1 * 1000, 0.00001f, this);
mintime =1000ms always it is calling ....

What does this line of code do..?

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1200000, 200, ll);
My assumption is that it gets the location every 120000ms if it has changed more then 200 meters.
At least this is what I am experiencing anyway....which is what I want. But what happens if the phone ends up in a metal building where there is NO GPS reception..??
Does it just keep trying every 1200000ms for a GPS fix forever..?? Or does this eventually die off and not work again..??
If I put the phone on my dash and drive around...every 120000ms I send a SMS message with the latest coordinates. Everything works just fine. But if the phones ends up in a building for 3-4-5 hours and comes back out side 3-4-5 hours later....my hope is that it will pick back up where it left off and send me a new location (if it has changed more than 200 meters). But this does not appear to be the case. It just stops working and I never get a new location even if I do have a clear view of the sky. Just wondering if this stops searching for a GPS fix or something after so many attempts to obtain a GPS fix...?
It sounds like you are searching for the Critera class.
You basically define a set of criteria which then is used to get the best suitable provider for this criteria. You then pass this criteria to the getProviders() method of the LocationManager class to get the best suitable provider as a string. Be it GPS, Network etc.
You can alternatively specify the fix provider manually by doing something like this
// Get a fix from the GPS provider
LocationProvider provider = LocationManager.GPS_PROVIDER;
// Get a fix from the Network provider
LocationProvider provider = LocationManager.NETWORK_PROVIDER;
There is a very interesting read on the developers site about obtaining users location you might want to check out. Especially the part about deciding when to start listening might be of interest for you but I recommend reading the whole article.
Using the Network provider will get your best possible position without a GPS fix in other words also inside a building. You have to keep in mind tho that the position fix you obtain might be quite off of the real position when querying the network provider.
The method you are asking for requestLocationUpdates(String provider, long minTime, long minDistance, LocationListener listener) will request a location update from the set provider every minTime milliseconds or every minDistance meters and inform the listener upon that.
Hope it helps.
Did you check this site?
Specifically this link: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.app.PendingIntent)
void requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent) Registers the current activity to be notified periodically by the named provider.void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) Registers the current activity to be notified periodically by the named provider.void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper) Registers the current activity to be notified periodically by the named provider.But the call you're showing uses an int as last parameter. That's not a documented function.

Categories

Resources