Find nearby places android java - android

I have a list of latitude and longitude stored in database. Now I'm trying to develop a function on finding nearby place from my current location. Means that, i have to search all the latitude and longitude from my database and see which of the places are within my area will be shown.

Location.distanceBetween(lat1,lon1, lat2,lon2, result);

To find the distance from one two-dimensional point to another you can use the Haversine formula, (without calling any API functions.)
// All measurements are in meters
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(coord1, coord2) {
// This is the Earth's radius
var earthRad = 6378137;
var dLat = rad(coord2.lat() - coord1.lat());
var dLong = rad(coord2.lng() - coord1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(coord1.lat())) * Math.cos(rad(coord2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var b = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance = earthRad * b;
// Returns distance from coord1 to coord2 in meters
return distance;
};
Your question is a bit broad so this might not be the best solution. If you were constantly updating your current location you'd have to implement a Tree Sorting algorithm or some other way of optimizing performance rather than checking through your database for distances on every update.

You can use the Location object. The distanceBetween or distanceTo method will give you the 'as the crow flies' distance between two points. From there you can filter your results and display only the places you want to.

here is what i use
public void nearBy() {
if (map.getMyLocation() != null) {//check if my location was found
db = new MyDatabase(MainActivity.context);
Cursor medecin = db.lireMedecin();//read values from database
map.clear();//clearing the map
while (medecin.getPosition() < medecin.getCount()) { //cheking if this doctor is nearby...
String Lat1 = medecin.getString(5);//doctor's latitude
String Lon1 = medecin.getString(6);//doctor's longitude
LatLng me = new LatLng(map.getMyLocation().getLatitude(), map.getMyLocation().getLongitude());//my location
LatLng med = new LatLng(Double.parseDouble(Lat1), Double.parseDouble(Lon1));//doctor location
if (CalDist(me, med) < 6) {//if distance is < 6km add marker to the map
map.addMarker(new MarkerOptions().position(med).title(medecin.getString(1) + " " + medecin.getString(2))
.snippet(medecin.getString(4)).icon(BitmapDescriptorFactory
.fromResource(Icone(medecin.getString(7).charAt(0))))); //showing the doctor on the map
}
medecin.moveToNext();//next doctor
}
} else
Toast.makeText(context, "Gps signal not valid", Toast.LENGTH_SHORT).show();//can't find my position
}
calculate distance :
public static double CalDist(LatLng StartP, LatLng EndP) {
int Radius = 6371;//­radius of earth in Km 
double lat1 = StartP.latitude ;
double lat2 = EndP.latitude ;
double lon1 = StartP.longitude ;
double lon2 = EndP.longitude ;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}

Related

Find if user is within given distance of given Coords

NOTE: I DO NOT need to monitor the user location continuously, just perform the check once off on a button click
I have a Set of Latitude and Longitude, and need to check if the user is within given distance of that point, how can I do it? I checked out Geofencing, but it works like a service, while I need to perform the check once in the main thread
just check with it.
for a location having latitude and longitude set boundaries with the below code:
public LatLngBounds getLocationBounds(LatLng sourceLocation, double distance)
{
LatLng southwest = SphericalUtil.computeOffset(sourceLocation, distance, 225);
LatLng northeast = SphericalUtil.computeOffset(sourceLocation, distance, 45);
return new LatLngBounds(southwest, northeast);
}
This is the method used to calculate the distance between two points:
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}
This is also a duplicate of this question: Find distance between two points on map using Google Map API V2
Please try to see if your question has been answered before asking in future.

Using Google maps api how to cmpare GPS coordinates(latti , longi) of two locations

In an Android application I am struck in a problem any one have information about GPS coordinates from map , please help.
The problem is if I have coordinates(latitude and longitude) of two different points on Map and
I want to check that the points are on the same location on map
Secondly are the two points are nearer to each other according to any criteria
For example .. if the point is in the 100m radius of the other point it seems to be nearer.
You can use haversine formula to calculate two points,
public static void main(String[] args) {
// TODO Auto-generated method stub
final int R = 6371; // Radious of the earth
Double lat1 = Double.parseDouble(args[0]);
Double lon1 = Double.parseDouble(args[1]);
Double lat2 = Double.parseDouble(args[2]);
Double lon2 = Double.parseDouble(args[3]);
Double latDistance = toRad(lat2-lat1);
Double lonDistance = toRad(lon2-lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
Double distance = R * c;
System.out.println("The distance between two lat and long is::" + distance);
}
You can compare this distance.This will do your job.

Android Find Latitude Longitude Of X point From Defined Location

i m working on Android MapView and developing a map based Application. i Need to find an X distance from the Specific Co-ordinates. Direction is not my priority Distance is my priority let say i need to find 100 meters from a particular location any idea on how can i do that
Thanks in advance for reading and answering.
in order to calculate to find a point on a line a given distance away from an origin, you need to have a bearing (or direction) as well as the distance. Here is a function that will take a starting location, a bearing and a distance (depth) and return a destination location (for Android): You may want to conver it from KM to Meters or whatever.
public static Location GetDestinationPoint(Location startLoc, float bearing, float depth)
{
Location newLocation = new Location("newLocation");
double radius = 6371.0; // earth's mean radius in km
double lat1 = Math.toRadians(startLoc.getLatitude());
double lng1 = Math.toRadians(startLoc.getLongitude());
double brng = Math.toRadians(bearing);
double lat2 = Math.asin( Math.sin(lat1)*Math.cos(depth/radius) + Math.cos(lat1)*Math.sin(depth/radius)*Math.cos(brng) );
double lng2 = lng1 + Math.atan2(Math.sin(brng)*Math.sin(depth/radius)*Math.cos(lat1), Math.cos(depth/radius)-Math.sin(lat1)*Math.sin(lat2));
lng2 = (lng2+Math.PI)%(2*Math.PI) - Math.PI;
// normalize to -180...+180
if (lat2 == 0 || lng2 == 0)
{
newLocation.setLatitude(0.0);
newLocation.setLongitude(0.0);
}
else
{
newLocation.setLatitude(Math.toDegrees(lat2));
newLocation.setLongitude(Math.toDegrees(lng2));
}
return newLocation;
};
Just to make the answer from javram into meters and radians instead of degrees.
/**
* Create a new location specified in meters and bearing from a previous location.
* #param startLoc from where
* #param bearing which direction, in radians from north
* #param distance meters from startLoc
* #return a new location
*/
public static Location createLocation(Location startLoc, double bearing, double distance) {
Location newLocation = new Location("newLocation");
double radius = 6371000.0; // earth's mean radius in m
double lat1 = Math.toRadians(startLoc.getLatitude());
double lng1 = Math.toRadians(startLoc.getLongitude());
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(distance / radius) + Math.cos(lat1) * Math.sin(distance / radius) * Math.cos(bearing));
double lng2 = lng1 + Math.atan2(Math.sin(bearing) * Math.sin(distance / radius) * Math.cos(lat1), Math.cos(distance / radius) - Math.sin(lat1) * Math.sin(lat2));
lng2 = (lng2 + Math.PI) % (2 * Math.PI) - Math.PI;
// normalize to -180...+180
if (lat2 == 0 || lng2 == 0) {
newLocation.setLatitude(0.0);
newLocation.setLongitude(0.0);
} else {
newLocation.setLatitude(Math.toDegrees(lat2));
newLocation.setLongitude(Math.toDegrees(lng2));
}
return newLocation;
}

Calculate bearing between two locations (lat, long)

I'm trying to develop my own augmented reality engine.
Searching on internet, I've found this useful tutorial. Reading it I see that the important thing is bearing between user location, point location and north.
The following picture is from that tutorial.
Following it, I wrote an Objective-C method to obtain beta:
+ (float) calculateBetaFrom:(CLLocationCoordinate2D)user to:(CLLocationCoordinate2D)destination
{
double beta = 0;
double a, b = 0;
a = destination.latitude - user.latitude;
b = destination.longitude - user.longitude;
beta = atan2(a, b) * 180.0 / M_PI;
if (beta < 0.0)
beta += 360.0;
else if (beta > 360.0)
beta -= 360;
return beta;
}
But, when I try it, it doesn't work very well.
So, I checked iPhone AR Toolkit, to see how it works (I've been working with this toolkit, but it is so big for me).
And, in ARGeoCoordinate.m there is another implementation of how to obtain beta:
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second {
float longitudinalDifference = second.longitude - first.longitude;
float latitudinalDifference = second.latitude - first.latitude;
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
if (longitudinalDifference > 0)
return possibleAzimuth;
else if (longitudinalDifference < 0)
return possibleAzimuth + M_PI;
else if (latitudinalDifference < 0)
return M_PI;
return 0.0f;
}
It uses this formula:
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
Why is (M_PI * .5f) in this formula? I don't understand it.
And continue searching, I've found another page talking about how to calculate distance and bearing of 2 locations. In this page there is another implementation:
/**
* Returns the (initial) bearing from this point to the supplied point, in degrees
* see http://williams.best.vwh.net/avform.htm#Crs
*
* #param {LatLon} point: Latitude/longitude of destination point
* #returns {Number} Initial bearing in degrees from North
*/
LatLon.prototype.bearingTo = function(point) {
var lat1 = this._lat.toRad(), lat2 = point._lat.toRad();
var dLon = (point._lon-this._lon).toRad();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x);
return (brng.toDeg()+360) % 360;
}
Which one is the right one?
Calculate bearing
//Source
JSONObject source = step.getJSONObject("start_location");
double lat1 = Double.parseDouble(source.getString("lat"));
double lng1 = Double.parseDouble(source.getString("lng"));
// destination
JSONObject destination = step.getJSONObject("end_location");
double lat2 = Double.parseDouble(destination.getString("lat"));
double lng2 = Double.parseDouble(destination.getString("lng"));
double dLon = (lng2-lng1);
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
double brng = Math.toDegrees((Math.atan2(y, x)));
brng = (360 - ((brng + 360) % 360));
Convert Degrees into Radians
Radians = Degrees * PI / 180
Convert Radians into Degrees
Degrees = Radians * 180 / PI
I know this question is old, but here is an easier solution:
float bearing = loc1.bearingTo(loc2);
Try this for accurate result:
private static double degreeToRadians(double latLong) {
return (Math.PI * latLong / 180.0);
}
private static double radiansToDegree(double latLong) {
return (latLong * 180.0 / Math.PI);
}
public static double getBearing() {
//Source
JSONObject source = step.getJSONObject("start_location");
double lat1 = Double.parseDouble(source.getString("lat"));
double lng1 = Double.parseDouble(source.getString("lng"));
// destination
JSONObject destination = step.getJSONObject("end_location");
double lat2 = Double.parseDouble(destination.getString("lat"));
double lng2 = Double.parseDouble(destination.getString("lng"));
double fLat = degreeToRadians(lat1);
double fLong = degreeToRadians(lng1);
double tLat = degreeToRadians(lat2);
double tLong = degreeToRadians(lng2);
double dLon = (tLong - fLong);
double degree = radiansToDegree(Math.atan2(sin(dLon) * cos(tLat),
cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(dLon)));
if (degree >= 0) {
return degree;
} else {
return 360 + degree;
}
}
You can test bearing result on http://www.sunearthtools.com/tools/distance.php .
In the formula
float possibleAzimuth = (M_PI * .5f) - atan(latitudinalDifference / longitudinalDifference);
the term (M_PI * .5f) means π/2 which is 90°. That means that it is the same formula that you stated at first, because regarding to the figure above it holds
β = arctan (a/b) = 90° - arctan(b/a).
So both formulas are similar if a refers to the difference in longitude and b in the difference in latitude. The last formula calculates again the same using the first part of my equation.
a in the diagram is the longitude difference, b is the latitude difference therefore in the method you have written you've got them the wrong way round.
a = destination.latitude - user.latitude; // should be b
b = destination.longitude - user.longitude; // should be a
Try switching them and see what happens.
See Palund's response for answers to the rest of your questions.
/*
Kirit vaghela answer has been modified..
Math.sin gives the radian value so to get degree value we need to pass Math.toRadians(value) inside Math.sin() or Math.cos()
*/
double lat1 = 39.099912;
double lat2 = 38.627089;
double lng1 = -94.581213;
double lng2 = -90.200203;
double dLon = (lng2-lng1);
double x = Math.sin(Math.toRadians(dLon)) * Math.cos(Math.toRadians(lat2));
double y = Math.cos(Math.toRadians(lat1))*Math.sin(Math.toRadians(lat2)) - Math.sin(Math.toRadians(lat1))*Math.cos(Math.toRadians(lat2))*Math.cos(Math.toRadians(dLon));
double bearing = Math.toDegrees((Math.atan2(x, y)));
System.out.println("BearingAngle : "+bearing);
If you want you can take a look at the code used in mixare augmented reality engine, it's on github and there's an iPhone version as well: github.com/mixare
inputs are in degrees.
#define PI 3.14159265358979323846
#define RADIO_TERRESTRE 6372797.56085
#define GRADOS_RADIANES PI / 180
#define RADIANES_GRADOS 180 / PI
double calculateBearing(double lon1, double lat1, double lon2, double lat2)
{
double longitude1 = lon1;
double longitude2 = lon2;
double latitude1 = lat1 * GRADOS_RADIANES;
double latitude2 = lat2 * GRADOS_RADIANES;
double longDiff= (longitude2-longitude1) * GRADOS_RADIANES;
double y= sin(longDiff) * cos(latitude2);
double x= cos(latitude1) * sin(latitude2) - sin(latitude1) * cos(latitude2) * cos(longDiff);
// std::cout <<__FILE__ << "." << __FUNCTION__ << " line:" << __LINE__ << " "
return fmod(((RADIANES_GRADOS *(atan2(y, x)))+360),360);
}

How to calculate distance using coordinates from LocationManager

I am using LocationManager to get the values of Latitude and Longitude of a user. These values are updated regularly to a database.
Now, i want to find out the distance between two users basing on the stored Latitude and Longitude values. I want to display a message when distance between two users is less than (say 100 meters). Can anyone please guide me with a tutorial or some sample code of how to achieve it.
You can have a look at the Location class. You can set the longitude and latitude and there is a distanceTo method that you can use
Ou pode utilizar este metodo que eu desenvolvi...
Or you can use this method I developed...
public static Double distance(latitudeA, latitudeB, longitudeA, longitudeB) {
double radius = 3958.75;
double dlat = ToRadians(Double.parseDouble(String.valueOf(latitudeB))
- Double.parseDouble(String.valueOf(latitudeA)));
double dlon = ToRadians(Double.parseDouble(String.valueOf(longitudeB))
- Double.parseDouble(String.valueOf(longitudeA)));
double a = Math.sin(dlat / 2)
* Math.sin(dlat / 2)
+ Math.cos(ToRadians(Double.parseDouble(String.valueOf(latitudeA))))
* Math.cos(ToRadians(Double.parseDouble(String.valueOf(latitudeB)))) * Math.sin(dlon / 2)
* Math.sin(dlon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Double d = radius * c;
double meterConversion = 1609.00;
return d * meterConversion;
}
private static double ToRadians(double degrees) {
double radians = degrees * 3.1415926535897932385 / 180;
return radians;
}

Categories

Resources