In my android app I'm trying to compare user location with a list of stores coordinates.
I already know how to get user coordinates and I already have the list as a array.
How can I compare both and find the nearest store?
float shortestDistance = Float.MAX_VALUE;
Location closestLocation = null;
for(Location loc : locations){
if(yourLocation.distanceTo(loc) < shortestDistance){
shortestDistance = yourLocation.distanceTo(loc);
closestLocation = loc;
}
}
Related
This is how i calculate the Distance between my list.
my code is below
public void calc(){
for(int i = 0; i < mSearchResultModelsToGetAppointments.size(); i++){
Location newLocation = new Location("");
newLocation.setLatitude(gpsTracker.getLatitude());
newLocation.setLongitude(gpsTracker.getLongitude());
if(String.valueOf(gpsTracker.getLatitude()).equalsIgnoreCase("0.0") || String.valueOf(gpsTracker.getLongitude()).equalsIgnoreCase("0.0")) {
} else{
Location oldLocation = new Location("");
oldLocation.setLatitude(mSearchResultModelUnderProcess.getLatitude());
oldLocation.setLongitude(mSearchResultModelUnderProcess.getLongitude());
float distanceInMeters = newLocation.distanceTo(oldLocation)/1000;
if(distanceInMeters < 2820){
getAdapter().notifyDataSetChanged();
getAdapter().removeItem(i);
getAdapter().notifyDataSetChanged();
}
}
}
}
This is how i display the location now.
If you do not want use any external library, I would suggest following way.
Create your own custom Location Class with attributes lat and lng, and additionally attribute called distance which measures from your current location till the point. here you can find how to find distance between 2 points
This class should implement Comparable interface and override its compare function. Inside this function, you can create your own sorting logic based on distance
here you can find how to use comparable interface, Let me know if you have still problem
Happy coding
i am trying to work on application , in which i have List of location from database and i want to check Which location from database is near to my current location within few miles like 20 miles and list got refreshed automatically with the refined Location and then send notification to user that you have enter to this location...
I have searched over this and come to know about Geofencing But i did not get it properly.. so please suggest me how should i start working on it ... Any sample , code or link will be helpful
thanks in advance
I have used one function distanceTo to calculate distance between to Locations.
I have coded it for only 100 mtr, you need to change value of distance you want to compare with.
private boolean isMarkerIn100(ArrayList<Model> arrayList) {
Location locationA = new Location("LocationA");
locationA.setLatitude(latitude);
locationA.setLongitude(longitude);
for (int i = 0; i < arrayList.size(); i++) {
Location locationB = new Location("LocationB");
locationB.setLatitude(arrayList.get(i).getLat());
locationB.setLongitude(arrayList.get(i).getLng());
if (locationA.distanceTo(locationB) < 100 && arrayList.get(i).getHit() > 4) {
return false;
}
}
return true;
}
In above code, latitude and longitude are for my (User's) location (Say locationA) and ArrayList is data from database with lat and lng.
How to Compare saved (in database ) Geo Location(Longitude and Latitude) with currently getting one using GPS.i tried following link but it doesn't work for me ,getting wrong output.Please help me i am new in Android.
How to use coarse location to compare with saved location in database
my requirement is exact as explained in ANS.
i did
float radius = 150; // distance in meter
double latitude_new= gps.getLatitude();
double longitude_new = gps.getLongitude();
Location location1= new Location("gpslocation");
location1.setLatitude(21.xxxxxxx);
location1.setLongitude(78.xxxxxxx);
Location location2= new Location("gpslocation");
location2.setLatitude(latitude_new);
location2.setLongitude(latitude_new);
float distance = location2.distanceTo(location1);
distance=Math.abs(distance);
//comparing two distance and radius
if (distance <= radius){
Toast.makeText(MyActivity.this, "At home", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MyActivity.this,"Not in Home",Toast.LENGTH_SHORT).show();
}
problem is i m getting only "Not in Home" at SAME location which i have hard-coded and even on other locations(other than hard-coded).
thanks
You have provide latitude for the longitude also , edit your code as follows and try ,
location2.setLongitude(longitude_new);
I'm wrinting a application and I have to show the distance covered while I'm running.
I use the function "public void onLocationChanged" of the LocationListener. When the user tap a botton and start running I want to show the distance he covered updated to the point in which he is located.
I've written this code:
public void onLocationChanged(Location location) {
if(location != null) {
if(location.hasSpeed()){
if(latitude1 == 0 && longitude1 == 0){
latitude1 = (location.getLatitude()*Math.PI)/180;
longitude1 = (location.getLongitude()*Math.PI)/180;
} else{
latitude2 = (location.getLatitude()*Math.PI)/180;
longitude2 = (location.getLongitude()*Math.PI)/180;
distance = (6372.795477598)*Math.acos(Math.sin(latitude1)
*Math.sin(latitude2)+Math.cos(latitude1)
*Math.cos(latitude2)*Math.cos(longitude1-longitude2));
sumDistance += distance;
latitude1 = latitude2;
longitude1 = longitude2;
}
tv.setText("Distance covered=" + sumDistance + " m");
}
}
}
Is it accurated?
Just a Suggestion:
Store the Latitude and Longitude of the start location and the end location when the user clicks the appropriate button.
and then you could use distanceBetween or distanceTo to get the distance between those two geoPoints.
P.S: This may not work if the user will start and end his run at the same point ;)
Addition:
Check this tutorial:
Recently google has improved its location based API's. They have fused the sensors with the location based api's to share examples of how location can be made more accurate (about 4 times more effective) and consume much lesser power (10 times lesser).
Some interesting links for you to go through will be this and video google io.
SO link for how to use the API's here
I used the following code to find the longitude and latitude for my android application
public double[] getlocation() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i = 0; i < providers.size(); i++) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
I got the accurate or exact latitude and longitude first time when i run the application after i tried to get the new co-ordinates from one miles away from my first location but i got the same latitude and longitude.
so please can you suggest me how this problem occurs. what is the solution for this ?
.getLastKnownLocation() will be inaccurate. You need to use a LocationListener to get accurate and up to date location updates. See this link. You generally ONLY want to use getLastKnownLocation ONCE upon starting to get a quick fix, then register a LocationListener to get more accurate, constant updates.
Also be aware that sitting in your house attempting to test will not work very well. You might need to go outside and walk around some.