I am developing an app who makes basically the following things: when the GPS is enabled it takes the current position and calculates the distance between the location took from the location manager and a fixed position, then when the distance is lower than a value, for example 10 meters, the app automatically open another Activity containing the informations about the location reached, the problem is that when the user is visualizing the info page he remains in the range and the previous activity continues opening the same activity with informations, how can i prevent this problem? I need to stop the method that open the new Intent when the info are visualized and then restart when the user is back in the distance calculation Activity.
The concept is called proximity alerts. May be you have implemented this on your own and which is not working properly. Look into the implementation of Proximity Alerts. Moreover this will have two notifications when user enters into range and when exits from the range.
Related
From what I know, the Google API Geofence should automatically retrieve the location of the device based on some parameters (e.g. the distance from the area). However, I've been trying to implement the code provided here and run it on the virtual machine for several minutes, but the current location of the device is never updated.
So, it is not clear to me if the Geofence automatically and periodically retrieves the location of the device, or simply uses the last known location.
I want to get local notification on my android phone. For example- In starting, user might choose the range like 1km, 2km or 3km as per his own wish and saves this range with his current location(latitude and longitude). After some time user migrates from one location to another means he can go anywhere apart from his current location, his location gets changed. Now in this situation, when he returns towards his last saved location, notification should come on his phone because he is near from his last current location that he saved before when he started to go somewhere. This notification should come when he enters within the specified range by user from his prior saved current location. Answer will be appriciated !!!
This is a feature called GeoFencing.
Check the link for sample:
https://github.com/googlesamples/android-Geofencing
Check google docs and guides for more info:
https://developers.google.com/android/reference/com/google/android/gms/location/Geofence
https://developer.android.com/training/location/geofencing.html
I am making a geofencing application, which must trigger notification constantly while the device is outside the geofence, not only on exit.
I have tried using Geofencing API through the training example given in Google android developer training page. But, I got notification only when the device enters, dwell or exit.
According to what I have researched till now in both SO and google, there are only three types of transitions handled in Geofencing API: Enter, Dwell and Exit. And the notification starts only when device enters the geofence, till it exits.
Can anyone help me out how to make it work?
That is not possible that way. It does not make any sense outside the geofence area. That is not an usecase of geofence.
You can try to specify broader range of the area and then check the geocoordinates if this is outside of your area you want to have.
Incase you want your own custom exit trigger, when you are outside the fence. You can write your custom action by measuring distance(lat,long) between your current location(FusedLocationProvider) and the fence location (lat, long).
You can use below methods available in Android Location API to measure the difference and generate a custom trigger to indicate that you are outside the fence.
Typically this is what you need to do
if[ (difference in location) > (Radius of your geo fence) ] -> Custom Exit of Fence Action
distanceTo
distanceBetween (Provide both your current lat,long and your fence lat,long here)
I created an android app, which stores user's home location and count distance between home and his/her actual location, in a Service class.
Now I would like to programme feature that would decide based on counted distance, if a user is:
at home
away
getting closer
distancing from home
This should be similar to Google Now, that can predict that you are for example going home. If someone had some examples, I would be sooo grateful
To implement such kinda work, you need to implement GeoFence in your android app.
GeoFence gives you idea about user entered/exited from a defined area. If you set home location as base location and radius to 500m then it gives you entered callback you are within 500m of your home.
When you go away 500m from your home location then it gives you exited callback. To know about whether you are home location then at a interval of few minutes you can poll GPS coordinates then can identify whether you are at your home location.
https://developer.android.com/reference/com/google/android/gms/location/Geofence.html
I am making a small app, when the user clicks the button the app gets the users location and opens Google maps.
The problem is, if the users mobile doesn't have GPS enabled , then enables it and clicks the button again, this method will return null:
location = service.getLastKnownLocation(provider);
When I wait after enabling GPS and then click the button, the method will work correctly and return the location.
How can I solve this problem?
why need to wait some second after enable GPS,to work?
It's because when you enable GPS it'll fix position of Satellite and based on that you'll able to get Location.
It is obviously a default behaviour, when you stop GPS and start it again, there is a small gap between start and stop, at that point, GPS will do its task, to get satellite data and after that it will provide you location. If we want to drive fast then we must need to start from 1st gear, same behaviour GPS doing.
As the answers mention above, the device will get the location after a while. If you are in the middle of a flow, you can show a ProgressSpinner and poll getLastKnownLocation() for x seconds to get a location.
If you hadn't used GooglePlayServicesClient try this to get user location.
It is the fastest way to get current lat long.
For reference and code use the following link.
http://developer.android.com/training/location/retrieve-current.html
Hop i helped you.