How to check the locations using LatLng values? - android

I'm a beginner in android and my question is regarding about google map api v2.I drew a path using my current position and my destination and I got some locations of a bus which is travelling through my path. How can I check the buse's location LatLng values are matching with my path LatLng values???

Try this,
Location crntLocation=new Location("crntlocation");
crntLocation.setLatitude(currentLatitude);
crntLocation.setLongitude(currentLongitude);
Location newLocation=new Location("newlocation");
newLocation.setLatitude(server_latitude);
newLocation.setLongitude(server_longitude);
float distance = crntLocation.distanceTo(newLocation); in meters
//distance =crntLocation.distanceTo(newLocation) / 1000; // in km
if(distance<200){
//You are within 200 meter range
}
Put this in a loop.

Related

How to get latitude and longitude at given bearing and given distance from a point

Let say I have point A(28.513393, 77.072276) and bearing is 45 degree then get point B(?,?) at distance 5 Km from point A at given bearing.
In order to calculate the position of point B you can use the
Google Maps Android API Utility Library. The utility library has a class SphericalUtil that allows to calculate distances, areas and headings via spherical geometry.
The method of SphericalUtil that should be used is the computeOffset. According to the documentation
public static LatLng computeOffset(LatLng from,
double distance,
double heading)
Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).
source: http://googlemaps.github.io/android-maps-utils/javadoc/
So, the code snippet in your example is
LatLng pointA = new LatLng(28.513393, 77.072276);
double distance = 5000.0; //expressed in meters
double heading = 45.0 //expressed in degrees clockwise from north
LatLng pointB = SphericalUtil.computeOffset(pointA, distance, heading);
For more details follow setup guide of the utility library:
https://developers.google.com/maps/documentation/android-api/utility/setup
I hope this helps!

Android GPS accuracy in calculating distance issues

The problem I believe is well known.
So what is my problem?
I am writing an app that is using the GPS provider to find the location of the user and calculate the distance from the user to the end point. This works correctly. However a problem occurs:
The problem in details:
If you go around the city with my app it will give you some numbers for your distance but ... the distance is aways changing. No matter if I move towards my destination the distance either goes higher or goes lower (the value of the distance calculated is "jumping" around from high to low and vise versa) the the previous number of the distance but logically it should be getting lower. I believe this is because the GPS signal is sometimes lost or weak and it cant calculate the distance correctly.
What I need help with?
I want to know is there a way to filter the coordinates received from the GPS so I can get more accurate numbers for distance so when I move towards my end point the distance is calculated correctly(as possible not necessary to be 100% correct) and not go up and down the scales like crazy.
How do I get the coordinates and calculate the distance:
public void onLocationChanged(Location location)
{
txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
clat = location.getLatitude();
clong = location.getLongitude();
Location location1 = new Location("Start");
location1.setLatitude(clat);
location1.setLongitude(clong);
Location locationB = new Location("Finish");
locationB.setLatitude(endLatitude); //endpoint coordinates
locationB.setLongitude(endLongitude);
distance = location1.distanceTo(locationB); //calculate the distance
TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");
CurLat = location.getLatitude();
CurLong = location.getLongitude();
The below answer can be further improved by saving the last, say, 10 location objects and do calculations based on those. If for instance the 10 last locations suggests that the user is moving towards the target at 1m/s, then a new location suggesting a jump of 5 meters is very likely inaccurate and should be ignored.
To filter some of the GPS updated which are way off you could simple do something like this (3 represent how accurate the position should be in respect to your actual position and may be adjusted):
public void onLocationChanged(Location location)
{
if (location.hasAccuracy() && location.getAccuracy() < 3) {
txtLat = (TextView) findViewById(R.id.currentCoordinatesView);
clat = location.getLatitude();
clong = location.getLongitude();
Location location1 = new Location("Start");
location1.setLatitude(clat);
location1.setLongitude(clong);
Location locationB = new Location("Finish");
locationB.setLatitude(endLatitude); //endpoint coordinates
locationB.setLongitude(endLongitude);
distance = location1.distanceTo(locationB); //calculate the distance
TextView TextDistance = (TextView)findViewById(R.id.TextDistance);
TextDistance.setText(new DecimalFormat("##,###,###.##").format(distance)+" m");
CurLat = location.getLatitude();
CurLong = location.getLongitude();
}
}
Here is the definition of the accuracy measure from: Location getAccuracy()
Get the estimated accuracy of this location, in meters.
We define accuracy as the radius of 68% confidence. In other words, if you draw a circle centered at this location's latitude and longitude, and with a radius equal to the accuracy, then there is a 68% probability that the true location is inside the circle.
In statistical terms, it is assumed that location errors are random with a normal distribution, so the 68% confidence circle represents one standard deviation. Note that in practice, location errors do not always follow such a simple distribution.
This accuracy estimation is only concerned with horizontal accuracy, and does not indicate the accuracy of bearing, velocity or altitude if those are included in this Location.
If this location does not have an accuracy, then 0.0 is returned. All locations generated by the LocationManager include an accuracy.

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 check if one location coordinates fall to another location coordinates radius in android

I have latitude and longitude of a current location. I want to check if target location (latitude and longitude) is close with 50 meter radius to the current location.
I am new to android.if anyone have solution about this.please reply.
use the built in distance calculation:
Location loc;
.....
float radius = 50.0;
float distance = loc.distanceTo(loc2);
if (distance < radius) then inside.

How to use coarse location to compare with saved location in database

I am writing an android app where a user will geolocate using coarse location using Network Location. I want the user to be able to save a location at a particular time into a db (savedLocation), and then if the user returns to savedLocation, (or within a coarse range of that location since i'm suing network location) I want to fire to a broadcast receiver. I know fine and well how to save the user's location to the db, and to compare it with the current location, etc etc.
However, given that the network location is a relatively inaccurate method for determining location, what would be a good approach for determining when the user approaches within distance X of that savedLocation.
What understand from your question is that you are tracking the user's location and saving that locations to the database. Now you want to prompt user when user reach to the previous location but there should be a specific region. If this is then you can use the below code to know the region of the user from latitude and longitude.
String latitude1, latitude2, longitude1, longitude2;
//Now Suppose you have some values in this variables.
//In your case latitude1 and longitude1 is from database and latitude2 and longitude2 is from current Location.
float radius = 500; //Distance in meters
Location location1 = new Location("GPS");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);
Location location2 = new Location("GPS");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);
Float distance = location1.distanceTo(location2);
distance = Math.abs(distance);
//Here you can compare two distances 1. distance and 2. radius
if(distance <= radius) {
//Use is in a 500 meters radius. You can notify user by notification
}

Categories

Resources