Recently, I have developed an app on travelling domain. The aim of this app is to show the user; the path from source to destination. When the user comes in the range of 25 meters range of the destination, the user gets a notification/ an alert stating that the destination is nearby.
What I tried to achieve it:
In the onLocationChanged() I kept on for the destination's range & if the user is in the range, the notification/alert will pop-out. However, when the I tested the app, I found out that when I am in the range, the notifications flood the device horribly as the condition of showing the notification/alert is based on the onLocationChanged() ie as location changes, the loop executes the exact same number of times and the user get annoyed by the app.
Also, the app does not work when I search for a different location. It does not show the destination marker. For the first time, the destination marker is seen but later searches do not show the destination marker; I wonder why?!
This problem has bugged me since long time. Please help me on this one!!
you can keep a check on the number of time the notification is displayed, for example in loop
boolean notification_shown = false;
for(...)
{
if(!notification_shown)
{
//show notification
notification_shown = true;
}
else
{
//rest of your coding
}
}
this will show the notification only one time. If you want to display notification for few more times than you can use counter. like
int counter = 0;
//increment it till you want and than stop
Two possible solutions that are probably best combined: one, use a static dialog so that you only have one instance of the alert; two, unregister the onLocationChanged() listener as soon as you reach the destination (the first time it alerts the user).
When you are using loop in the onLocationChange() method, in side your loop take a variable say "count" and then check the condition for the distance. After the particular condition got true then increment the count. If counter get 1 then pop an alert and after that exit from the loop.
int count=0;
public void onLocationChanged(Location locFromGps) {
if(locFromGps<=30)
{
if(count==1)
{
exit(0);
}
count++;
Toast.makeText(getApplicationContext(),"Location is near", 0).show();
}
}
Related
I am trying to move the screen (map activity) of the Android app to the current location of the user while the map activity is running. So I need to call googleMap.moveCamera twice in 2 consequent lines to make it work. I cannot explain this... First time I call it with the test location from Google tutorial. The second line calls it with the current location of the user. What is the trick?)
If I exclude the first call of googleMap.moveCamera(CameraUpdateFactory), the screen with the map just shows some random place in the ocean...
This cannot be explained by the delay with the current location determining. Because the code is called in onSuccessListener AFTER the fusedLocationProvider provided us with the current location...
#SuppressLint("MissingPermission")
private fun moveCameraToTheCurrentLocation(googleMap: GoogleMap){
LocationServices.getFusedLocationProviderClient(this).lastLocation
.addOnSuccessListener { location: Location? ->
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(-33.88, 151.21), 15f)) //TODO: this is a plug. for some reason in the next line the camera does not move without this line
googleMap.moveCamera(CameraUpdateFactory.newLatLng(
LatLng(location?.latitude!!, location?.longitude!!)))
googleMap.animateCamera(CameraUpdateFactory.zoomTo(18.0f))
}
}
I am developing an android application in which I need to get my current Location. I have successfully wrote the code and I am getting my current location using Google Play Service.
The problem is sometimes it gives me the location after a long time. I have noticed that it was only for first use of the app.
Any way to avoid this problem and get the current location fast? Is it related to the version of google play service in my code? (I am not using the last one in fact I am using version 9.8.0.)
As #tahsinRupam said, avoid using getLastLocation as it has a high tendency to return null. It also does not request a new location, so even if you get a location, it could be very old, and not reflect the current location. You might want to check the sample code in this thread: get the current location fast and once in android.
public void foo(Context context) {
// when you need location
// if inside activity context = this;
SingleShotLocationProvider.requestSingleUpdate(context,
new SingleShotLocationProvider.LocationCallback() {
#Override public void onNewLocationAvailable(GPSCoordinates location) {
Log.d("Location", "my location is " + location.toString());
}
});
}
You might want to verify the lat/long are actual values and not 0 or something. If I remember correctly this shouldn't throw an NPE but you might want to verify that.
Here's another SO post which might help:
What is the simplest and most robust way to get the user's current location on Android?
when trying start navigation in some indoor environment, it will unable receive the GPS signal or detect with very slow, but when using other navigation app(Waze/Google Map/Here) the problem does not occur.
And also some indoor place can receive the gps signal without any problem.
below is my code for PositionListener
naviManager.addPositionListener(new WeakReference<NavigationManager.PositionListener>(positionListener));
private NavigationManager.PositionListener positionListener = new NavigationManager.PositionListener() {
#Override
public void onPositionUpdated(GeoPosition loc) {
// the position we get in this callback can be used
// to reposition the map and change orientation.
loc.getCoordinate();
loc.getHeading();
loc.getSpeed();
// also remaining time and distance can be
// fetched from navigation manager
naviManager.getTimeToArrival(true, Route.TrafficPenaltyMode.DISABLED);
naviManager.getDestinationDistance();
}
};
How are you starting the positionManager, maybe you have only passed the GPS option and not the GPS_NETWORK option ?
positioningManager.start(PositioningManager.LocationMethod.GPS_NETWORK);
Trying to figure out how to do this, basically my App requires a button to be hit and hit multiple times, it counts how many times you hit it and if you don't hit one within a certain space of time it will display a message.
I just can't figure out how to get the 'If button isn't pressed within 'x' seconds then...' part, I've tried if(imagebutton1.isPressed()) statement but it checks it instantly when the actvity starts, I just want it to check 'X' amount of seconds after the button was last pressed.
ANy help is appreciated thanks.
In your case you would need to record the last time the button was pressed
Then add a updated while statement
In c++
Int presses;
Int timelimit; //the seconds between each press (you can use a chronometer but this is simpler but less accurate (and no decimals)
Int lastpressed; //need chronometer for more accuracy or decimals)
Int ammountpassed; //time since you pressed it
If(imagebutton1.isPressed())
{
Bool Started = Yes;
Presses++;
While(!imagebutton1.isPressed()&&ammountpassed<TimeLimit)
{
Ammountpassed++;
};
};
If (ammountpassed>=timelimit)
{
If (presses>=highscore)
{
DisplayMsg " Number of presses" presses; "! New highscore!";
};
Else(presses<highscore)
{
DisplayMsg "not fast enough! Number of presses" presses; "!" };
};
};
You will have to tweak it a bit to fit your needs ("displaymsg" I for think is the actual function so you might have to change that but there's the logic :)
I recommend hand typing this as I belive I may have made a few error but nothing adding a semi colon or 2 won't fix ;)
Hope it helps :) Good Luck :)
Every time the user hits the button you can post a message on the handler's queue with your text message to be displayed and the appropriate delay time (and remove previous messages). Therefore if the delayed time exceeds without any press the thread will execute the handler's message. Lets say you want to post to the main handler a message to be executed in delay number of milliseconds, then in your activity you would need to hold the reference to the handler and create a Runnable where the necessary text message will be displayed:
Handler mainHandler = new Handler(getMainLooper());
Runnable runnable = new Runnable(...);
In your OnClickListener of the button you would need to execute only:
#Override
public void onClick(View v) {
mainHandler.removeCallbacksAndMessages(null);
mainHandler.postDelayed(runnable, delay);
}
I have a working app that sorts places by distance from my location or alphabetically if the user does not want to enable GPS. Everything works well, but I would like to enhance my app by automatically sorting alphabetically if a satellite can not be reached, say from a basement, or if it is just taking over a given period of time, say 5 seconds. Is this even possible? I haven't been able to find anything like this. I'd like to just pop up a message that says "Can't reach satellite, sorting alphabetically" instead of the user having to do anything themselves.
Thanks for your responses and your time.
Mike
You should override your LocationListener's onStatusChanged() callback:
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (provider.equals(LocationManager.GPS_PROVIDER)){
if (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE){
// GPS unavailable: send notification
} else {
// you're out of a basement, continue using GPS
}
}
}
Android documentation promises that if a provider is unavailable at the moment you subscribe your LocationListener, it invokes this callback immediately.
Why not use some combination of Out of Service and Temporarily Unavailable in the Location Listener's onStatusChanged()?
http://developer.android.com/reference/android/location/LocationListener.html