how to set a tolerance to my event handler android - android

I'm trying to make a GPS Android app and am having trouble setting a destination point.
all the program does at the moment is grab your GPS location, and display your Latitude and Longitude, I want the app to tell you when you have reached a certain Latitude or Longitude but the coordinates don't stay at a steady number so I can never get the event to happen for more then a second before the coordinates change, I think I need the event to happen between two different coordinates, one being higher then the target coordinates and the other being lower but I can't figure out how to do that, someone please help

Specify a broader area that you'll accept.
if(lat > TARGET_LAT - 0.02 && lat < TARGET_LAT + 0.02 && lon > TARGET_LON - 0.02 && lon < TARGET_LON + 0.02){
// close enough!
}
See this for an idea on what the numbers should be.

Related

Check if latitude and longitude lies in geofenceing radius

I'm working on an application. I need geo-fencing to check whether a particular
latitude and longitude lies within a specific radius. I'm following a tutorial on geo-fencing but my problem is that I dont know where to pass the latitude and longitude that I want to check
Tutorial Link:
https://code.tutsplus.com/tutorials/how-to-work-with-geofences-on-android--cms-26639
Here
.setCircularRegion( LATITUDE, LONGITUDE, RADIUS)
When the IntentService detect the transition (enter, exit or dwell) on region specified the onHandleIntent() will be called and you can do your action (alert or anything)
#tkrz 's formula is correct.
(lat-center_lat)^2 + (lon - center_lon)^2 < radius^2
The left side is square of distance between my location to the geofence center point. The right part is square of radius of the geofence circle.
Lets say, if the distance is smaller than the radius of the circle, it means I am standing inside the geofence area, and vice versa.
If you only want to check this (not using mechanism for notifications) you will need some math:
(lat-center_lat)^2 + (lon - center_lon)^2 < radius^2

Location.distanceTo not giving correct results in android

I am trying to calculate the distance between two locations (Current location with the previous location).
So I tried the following:
Location previousLocation = new Location("");
previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f));
previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f));
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
editor.putFloat("totalDistanceInMeters", totalDistanceInMeters);
Log.e("Location Update","totalDistance"+totalDistanceInMeters);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+totalDistanceInMeters/1609, Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
To test the above code. The first time result was perfect and when it triggered the second time. The phone was in the same location but I am getting distances like 141.0111 m. thrid time 304.0011 m. Am I doing something wrong here?
The results are not showing up correctly. According to doc online the results are in metres.
Is there an easy way to calculate the difference between the first location results with the second one and if it is more than 10m I would like to do some other calculation if not just keep quite.
Let me know.
why are you even using the following code
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
That adds up the previous distance to present distance and gives the added value everytime....
example
first time
distance between A and B is 100 m
second time
distance between A and B is 100 m+100 m=200 m
so try using distance directly in toast
float distance = location.distanceTo(previousLocation);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+distance/1609,Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
I think accuracy needs to be set in order to get the exact distance. Also try to get hold of the previous and current locations manually so as to calculate the distance and verify.

Android, using gps and compass to point towards coordinate

I'm trying to make an app that points you toward a position. You press a button and it stores the gps coordinates, then calculates things like distance and the angle you need to face. Then it leads you back to that remembered position by "pointing" toward it using an onscreen compass graphic.
At least, it's supposed to. After messing with the code for hours, I've come to the conclusion that there's just a logic error somewhere due to my lack of trig practice over the past few years.
The compass and GPS position are updated fairly frequently. This is the code in my main update call for the user interface that rotates the compass and displays the distance.
public void updateUI(){
double deltaX = targetLongitude - currentLongitude;
double deltaY = targetLatitude - currentLatitude;
double distance = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
double rotation = Math.toDegrees(Math.atan2(deltaX,deltaY));
distanceTextView.setText(Double.toString(distance));
rotateCompass(rotation - degreesClockwiseFromNorth);
}
and the code for rotateCompass:
public void rotateCompass(double degrees){
degrees -= currentRotation; //calculates necessary rotation across updates
currentRotation += degrees;
matrix.postRotate(
(float) degrees,
compass.getDrawable().getBounds().width() / 2,
compass.getDrawable().getBounds().height() / 2);
compass.setImageMatrix(matrix);
}
I'm almost certain my rotation code works because when I replace
rotateCompass(rotation - degreesClockwiseFromNorth);
with
rotateCompass(0 - degreesClockwiseFromNorth);
it points north right alongside a real compass regardless of the direction I'm facing. But when I use the former, it points towards a consistent point, but that point seems to be nowhere near the target point.
So I've come to the conclusion my error is either in calculating the correct angle, or expecting the gps to be too precise. I haven't tested it for distances further than what I can in my backyard, but I assume that if it was a gps accuracy issue I'd see my compass jumping all over the place rather than adamantly pointing in a wrong direction.
Thanks for reading, any suggestions or corrections are appreciated.
Your math is all screwed up because the distance between 2 degrees of longitude is not the same as 2 degrees of latitude. In fact, it isn't even a constant length for longitude- its shorted by the poles and longest at the equator. Use the Location.distanceTo functions instead.

Check if device is really moving

I am working on an application where I need to get the speed of a car. To get the speed, I know I can use something like double speed =locationB.getSpeed();` however when I am testing, the speed varies between 0.0 and 40 km/h when I am just sitting right behind my laptop not moving at all. In the car, the speed actually comes close to the cars speed, so that shouldn't be a problem.
What would be the best way to check if the device is really moving? I've already tried to get the distance between locationA and locationB and use that with the time it took to get the 2 locations, to get the speed.
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
However this seems to be not stable at all, like the getSpeed() method.
Is there a way to only display the speed if the device is moving? And would it be reliable?
Any help is appreciated,
Thanks.
Check the horicontal accuracy attribute of Location.
If it is under 30m you can ignore the location.
If you are sitting on your laptop and get speed = 40km/h (which I never saw in good GPS devices), then look what the hor. accuracy is.
It probably is much over 30m.
In GPS based systems, never ever calculate the speed by positional change in time,
just use the location.getSpeed().
The reason is that the GPS chip internally calculates the speed via physical doppler effect, not via positional change.
While standing still, or at very low speeds this does not work well, so you have to filter out very low speeds, and bad gps signal. (via horicontal accuracy estimate)
I think you should limit the distance between A and B to be a minimum length. Small distances will introduce more error into your speed calculations.
Boolean moving - false;
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
if (distance > SOME_THRESHOLD) {
moving = true
}

Android GPS for a Square region

I would like to use Androids Gps to as a access point on my app. Meaning if a user isn't with in a square. how do I get Android to accept between (for example: Longitude and Latitude) 50.000 - 65.010 (with 50.00, 50.00 as my Center point or Tower). Now because I want to cover a a 4 block radius how to I get android to accept values only between C to G for both gps values. I guess I want to prevent users from using application outside of city, state, neighborhood ... a square area. So far I am successful in using Gps as a login but I am stuck at hard coded locations:
if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
//------------------------------------Username below -------------------------------------Password below ---//
if(username.getText().toString().contains("XX.408") && password.getText().toString().contains("-XX.") ) {
Any input on how I can cover a Square are would be very Thankful. Thanks in advance.
Use rectange.inside(Point).
Set origin of rectangle to SW corner (x=longitude, y =latitude)
Set width to longitudinal difference.
Set height to latitudinal difference.

Categories

Resources