Check if latitude and longitude lies in geofenceing radius - android

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

Related

Google Maps, calculate latitude one centimetre away from specified longitude on the screen?

Suppose if a marker's longitude is 124.4567. How can I calculate the longitude which is 1 centimetre away to the left side of the screen? It will vary depending on the screen density and zoom level. Is there any inbuilt method to calculate that longitude?
PS: I am sorry, it was "longitude". I always am confused by the two. I have edited the question.
I am not trying to calculate geological distances between two markers. Basically, I want to know how much longitude is 1 centimetre on the screen (not 1 centimetre of actual land) of the device. I mean, 1cm on the screen could be 30 degree longitude difference if I have zoom it out on my phone, but 1cm on the screen could be 1 degree longitude on your phone if you have zoomed it in.
You got distance calculation between 2 coordinator from here:
Distance betwee
n coordinator
And you have destination long, source long, source lat, and distance just find out destination lat.
And about zoom level, you have scale ratio of google map here:
Google map scale ratio
"To the left side" means western direction. Actually, latitude varies from -90 to 90 degrees, so there is no latitude 124.
If you have any point LatLng, you can find point 1 meter to the left via simple math.
Radius of a parallel ring is r = R * cos(latitude_in_radians), so 1 meter takes 360 / r of longitude.
Therefore you can calculate your point as follows without any library.
var EarthR = 6378137;
var point = {lat: 45, lng: 124};
var point2 = {lat: point.lat, lng: point.lng - 360 / ( EarthR * Math.cos(point.lat * Math.PI / 180))};
console.log(point2);
After a lot of Google search, I have found this How to access Google Maps API v3 marker's DIV and its pixel position?. The answer showed how to convert screen locations to lat/lng coordinates. So I modified it a little to get the position 1 centimetre on the left, no matter what the zoom level is.
I put a marker on the left for visual debugging, when a marker is clicked. Here is the code. I am hoping it could be helpful to future people.
var dpi = resources.displayMetrics.densityDpi;
var pixelsInCm = (dpi/2.54).toInt();
var existingPoint = mMap!!.projection.toScreenLocation(marker.position);
var leftPoint = Point(existingPoint.x - pixelsInCm, existingPoint.y);
var leftLatLng = mMap!!.projection.fromScreenLocation(leftPoint);
//The code below is only for visual test. Not necessary.
var leftMarker = MarkerOptions()
leftMarker.position(leftLatLng);
leftMarker.title("1cm to the left");
mMap!!.addMarker(leftMarker);

How to get straight distance between two location in android?

First read Question carefully ...
I need straight distance, not by walking,car,or etc.
Take a look to this image which given below,
Google provide us distance by car and driving.
But I don't want it, I want straight distance between two location (latitude - longitude).
Which is displayed as as RED LINE.
NOTE : I don't want to put red line on Google map, just want the Distance in Units(mile,km,etc.)
ANDROID
double distance
Location locationA = new Location(“point A”)
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location(“point B”);
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
MATHEMATICALY
a = distance in degrees //meterConversion = 1609;
b = 90 - latitude of point 1
c = 90 - latitude of point 2
l = longitude of point 1 - longitude of point 2
Cos(a) = Cos(b)Cos(c) + Sin(b)Sin(c)Sin(l)
d = circumference of Earth * a / 360 // circumference of Earth = 3958.7558657440545D km
The Haversine function is used to find the distance between two points on a sphere.
It's fairly straightforward to extend this to finding the straight line distance between two points on the Earth. The Earth is not a perfect sphere, but this is still a good approximation using a standard measurement (called WGS84) for the radius at the equator.
As CommonsWare has said, you can do this very simply by using distanceBetween(), which uses the Haversine function and the WGS84 radius.
For better understanding of implementation/math, take a look at this sample code in Python.
Distance you find with following code.
You just need to get two geoPoint's latitude and longitude.
and use that in following calculation to get distance.
R = 6371; // km
d = Math.acos(Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lon2-lon1)) * R;
That will be return distance after all calculation.
R is the radius of surface in KM, need to use in calculation and you try this. I hope it is useful for you.

how to set a tolerance to my event handler 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.

Latitude / Longitude and meters

I have a small algorithmic problem.
I am developing an Android application. I get GPS coordinates. For example: latitude: 23.23907, longitude: 50.45786.
So I get a point. I want to compute bounds details on this point plus or minus 5 meters. I.e.:
[23.23907 - 5 meters ; 23.23907 + 5 meters]
[50.45786 - 5 meters ; 50.45786 + 5 meters]
How to make this calculation?
Thank you very much!
The haversine formula can be simplified a great deal when you work in north-south and east-west directions only.
If Earth's circumference is C, the point at d kilometers to south of a given point is 360*d/C degrees to the south. The point at d kilometers to east is 360*d/(C*cos(latitude)) degrees to the east. The cosine in the denominator comes from the fact that the length of the longitude at a given latitude shorter than the equator by that much.
So if the Earth's circumference is 40075.04 km, to move 5 m to the north/south you would add/subtract 0.0000449 from the latitude and use the same longitude. To move 5 m to the west/east you would use the same latitude and add/subtract 0.0000449/cos(latitude) to the longitude. Don't forget about the edge cases though: near poles you have to clamp latitude to 90°, and near longitude 180° you'll have too add or subtract 360° to keep the longitude in the correct range.
With your numbers the range turns out to be approximately:
latitude: [23.23903 ; 23.23911]
longitude: [50.45781 ; 50.45791]
Update: Note that this still assumes that the Earth is a perfect sphere, which it's not. The GPS system for example models the Earth as an ellipsoid where the equator is at 6378.137km and the poles are at 6356.7523142km from the center of the Earth. The difference is about 1/300th and matters a great deal for many applications, but in this case it's within the margin of error.
Correcting the formula for the longitude should be simple since the parallels are still circles: you would just have to swap cos(latitude) for the correct coefficient. Calculating the correct latitude is harder because the meridians are not circles but ellipses, and the arc length of an ellipse cannot be calculated using elementary functions, so you must use approximations.
I would like to add a very important comment:
The cosine is to be calculated on the latitude in radians and not in degrees.
conversion: radians = PI / 180 * degrees

point condition to be remove

If I 'm using GPS and adding some Over lay items in the map and i want to play a game when ever my current location is == to the over lay item then remove this item the problem is due to the gps inaccuracy i want to make a margin error around the over lay item so what i did is
Since the P is the geo point of the overlay item
enter code here
int error =10 ;
if (loc.getLatitude() * 1E6>=p.getLatitudeE6()-error&&loc.getLongitude() * 1E6 < p.getLatitudeE6()+error)
mapView.getOverlays().remove(itemizedOverlay);
mapView.invalidate();
so the question now is this if condition is true or what ??
Use LocationManager.addProximityAlert(double latitude, double longitude, float radius, long expiration, PendingIntent intent). The radius will be the distance you are to the POI.

Categories

Resources