I'm starting to create an application which will be show notification, when user will be within radius 1 km from my shop.
I have xml file, which show my shop lat and log:
<SHOPS>
<SHOP LON="19.456865000000000000" LAT="51.765639000000000000" CITY="90-423 Łódź" STR="ul. Piotrkowska 95" PHOTO=""/>
<SHOP LON="18.564883000000000000" LAT="54.443416000000000000" CITY="81-759 Sopot" STR="ul. Bohaterów Monte Casino 26" PHOTO=""/>
<SHOP LON="19.455248000000000000" LAT="51.770487000000000000" CITY="90-125 Łódź" STR="ul. Narutowicza 41" PHOTO=""/>
<SHOP LON="20.930370000000000000" LAT="52.241939000000000000" CITY="01-460 Warszawa" STR="ul. Górczewska 124" PHOTO=""/>
</SHOPS>
How can I do that, how to designate a radius of lat and long?
If you want to find the distance between two diferent locations (i.e. the user location and one of your shps location) you can use the following code:
Location shopLoc = new Location("");
shopLoc.setLatitude(shopLat); //get this value from your xml file
shopLoc.setLongitude(shopLon); //get this value from your xml file
Location userLoc = new Location("");
userLoc.setLatitude(userLat); //get this value from gps or other position device
userLoc.setLongitude(userLat); //get this value from gps or other position device
//Finaly get the distance with
float distance = userLoc.distanceTo(shopLocation)
Now you can compare the distance with 1 Km and show the notification
Related
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);
Currently I am working on an application, where I want to calculate distance a vehicle travels. My requirement is while driving car, my Android device should calculate total distance I traveled and send this information to server. In order to do this, I used Android's location manager api, set criteria and used getBestProvider. This way we can either use GPS or Network to get latitude and longitude. The following is the code snippet of this:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider,30000,5, this);
onLocationChanged() call back method provide latitude and longitude every time. We always store previous coordinates and when we get new coordinates we find the distance between the two using distanceBetween() api. The following is the code snippet of this:
public void onLocationChanged(Location location) {
findLatLongDistance(location);
}
private void findLatLongDistance(Location location) {
// TODO Auto-generated method stub
try{
Date date = new Date();
TimeStamp2 = sdf.format(date);
getSavedLatLong();//get lat and long from preference
Location locationB = new Location("point B");
locationB.setLatitude(location.getLatitude());
locationB.setLongitude(location.getLongitude());
Location locationA = new Location("point A");
locationA.setLatitude(prelat_val); //lat from pref locationA.setLongitude(prelong_val); //long from pref
if(prelat_val>0.0 && prelong_val>0.0){
Toast.makeText(LocationService.this,"Location Odometer Sum "+odometer_sum, Toast.LENGTH_LONG).show();
float distance2 = getDistance(prelat_val,prelong_val,location.getLatitude(),location.getLongitude());
odometer_sum = odometer_sum + (distance2/1000);
Toast.makeText(LocationService.this,"Lat "+prelat_val+"Long "+prelong_val+"Sum "+odometer_sum, Toast.LENGTH_LONG).show();
}
saveData(lat,lng,odometer_sum);
}catch(Exception e){
e.printStackTrace();
}
}
public float getDistance(double d, double e, double f, double g) {
float [] dist = new float[2];
Location.distanceBetween(d,e,f, g, dist);
return dist[0] ;
}
This is the list of issue that we face here:
The location it provide is not accurate. There is a difference of about 300-400 metres while we testing this app in 5 km distance
When the mobile is in same location for long time, it always provide different latitude and longitude. If you check above code snippet, in requestLocationUpdates(), we are setting 30 seconds time interval and 5m distance. Here what we thought is, if my mobile device move 5m distance AND if it cross 30 seconds interval, it will provide new latitude and longitude. But what really happens is, it provide coordinates every 30 seconds irrespective of device movement. I am not sure how to fix this issue.
While device is moving, how to get accurate value. Do I need to do some more things in the code?
I really spend so many hours trying various options. But I feel I am missing something here. Please help me on this. Thanks in advance..
Thanks,
Your minTime value in requestLocationUpdates() is 30seconds. Thats too long for an app that tries to calculate accurate distances. I have used locationManager.requestLocationUpdates(provider, 0, 5, locationListener);
in my code for long and I get accurate updates. Though this drains the battery very fast. So you would have to try different values to strike a balance between accuracy and battery life and see what suits your app
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;
}
}
This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}