I'm trying to calculate the distance between my Geolocation point and a marker tracing a polyline between these two, but the problem is when i output the result and it gives me an enourmous distance(1.125405E7) instead of (For Example) 1,250m.
This is my code
//Gets the latitude & longitude of my Geolocation
Location location = new Location("A");
location.setLatitude(location.getLatitude());
location.setLongitude(location.getLongitude());
//Gets the location of a marker/Polyline which i put using onMapLongCliclistener
Location locationb = new Location("B");
locationb.setLatitude(latLng.latitude);
locationb.setLongitude(latLng.longitude);
//Calculates the distance between Geolocation/Marker
double distance=locationb.distanceTo(location);
String distancia = String.valueOf(distance);
Toast.makeText(MapsActivity.this,distancia,
Toast.LENGTH_SHORT).show();
Any tip or solution?
Thank You Very Much!
From you code line no 3 'location.setLongitude(location.getLatitude());' is wrong. You are setting longitude instead of latitude.
Code:
double distance = SphericalUtil.computeDistanceBetween(new LatLng(location.getLatitude(),location.getLongitude()),new
LatLng(latLng.latitude,latLng.longitude));
String distanceC = String.valueOf(distance);
txtDistancia.setText(new DecimalFormat("##.##").format(distanceC)+" "+"mts");
Related
I'm trying to create an adndroid application which will shows a distance from moment when I started the app.
I'm using Location Manager and this is my idea:
Check coordinates.
(2 sec delay)
Chceck coordinates2
If coordinates2 != coordinates then calculate distance between them and add it to double distance.
Repeat
public void onLocationChanged(final Location location) {
sz = location.getLatitude();
dl = location.getLongitude();
String dls = String.valueOf(dl);
String szs = String.valueOf(sz);
aktualna_dlg.setText(dls);
aktualna_szg.setText(szs);
lat1 = location.getLatitude();
lon1 = location.getLongitude();
int secs = 2; // Delay in seconds
Utils.delay(secs, new Utils.DelayCallback() {
#Override
public void afterDelay() {
lat2 = location.getLatitude();
lon2 = location.getLongitude();
}
});
if (lat1!=lat2 || lon1!=lon2){
distance += DistanceCalculator.distance(lat1,lon1,lat2,lon2, "K");
}
if(distance<1000){
String distance_s = String.valueOf(distance*1000);
distance_tv.setText(distance_s + " m");
}
else if(distance>=1000){
String distance_s = String.valueOf(distance/1000);
distance_tv.setText(distance_s + " km");
}
}
But when I compile the app and catch GPS, I'm getting a distance ~ 6km.
What am I doing wrong?
I'm not sure what utils.delay does, since it isn't part of the framework. But assuming it works correctly- it happens on a delay. That means lat2 and lon2 won't update until its called. All of the code that uses those variables has to happen in afterDelay, they aren't valid until then.
Eliminate the delays and simply compare the argument to onLocationChanged with the argument from the last time it was called. As for why you're recording a huge distance traveled...
Use a high-pass filter
How far do you expect to move in 2 seconds? Probably less than your GPS receiver's accuracy, unless you're in an aircraft. Most of your "movement" is actually just random noise.
In one of my apps, I save the starting GPS location as a point of reference. On each call to onLocationChanged, I compute the distance between the current location and the point of reference. If the distance is larger than some high-pass value, I add the distance to the running total and set the new location as the point of reference. I use 100 meters as my high-pass value because I've seen a few devices with 100 meter accuracy or worse around tall buildings.
In my app I use the method "DistanceTo()" to get the distance between to places, but don't works correctly!! This is my code for this distance:
double latitudine;
double longitudine;
latitudine = 40.17;
longitudine = 24.9;
Location qui = new Location("Corrente");
qui.setLatitude(latitudine);
qui.setLatitude(longitudine);
double latitudine2;
double longitudine2;
latitudine2 = 40.16;
longitudine2 = 25;
Location due = new Location("Corrente2");
due.setLatitude(latitudine2);
due.setLatitude(longitudine2);
float b = qui.distanceTo(due);
Toast.makeText(getBaseContext(), "DESTINAZIONE KILOMETRI b:" + b, Toast.LENGTH_SHORT).show();
The toast show to me 11077.14 meters, but the distance should be 8.5 km!! as shown in these sites: http://www.movable-type.co.uk/scripts/latlong.html http://www.mapdevelopers.com/distance_from_to.php
In this case the difference is for about 3km, but sometimes the difference is sometimes the difference is even more!! Please help me! Why this code doesn't work correctly? Thanks
you are not calling setLongitude on both Locations, you need:
qui.setLongitude(longitudine);
due.setLongitude(longitudine2);
Might want to take a look at Point2D.Distance instead. It returns
the distance between the two sets of specified coordinates.
I am doing a GPS app, with latitude, longitude, speed, altitude...but I don't get any data for this one. I always get 1.0 as a value. (With the speed it happens the same).
I tried with the simulator of GPX in Eclipse (DDMS) and on the terminal (KML simulator, not on the street)...but it always shows 0.0 or 1.0.
My code is the following:
private Location currentLoc;
private Location previousLoc;
The criteria for the GPS...
// Criteria object is optional. Will return value representing "GPS"
Criteria oGPSSettings = new Criteria();
oGPSSettings.setAccuracy(Criteria.ACCURACY_FINE);
oGPSSettings.setSpeedRequired(true);
oGPSSettings.setAltitudeRequired(true);
oGPSSettings.setBearingRequired(false);
oGPSSettings.setCostAllowed(false);
oGPSSettings.setPowerRequirement(Criteria.POWER_MEDIUM);
When a new location is recieved...
#Override
public void onLocationChanged(Location location) {
currentLoc = location;
String sText = "Altitude: " + currentLoc.getAltitude();
txtOutput.setText(sText);
}
// sets this location as last location.
previousLoc = currentLoc;
}
I removed from the code things like displaying latitude, longitude...right now I'll show only altitude.
The question is: Why do you think is it happening this? The latitude and longitude works fine, but not the altitude. Maybe is it a hardware issue? Because the code seems correct, I think. Only with the getAltitude function should works.
Thank you in advance!
Did you ask for "android.permission.ACCESS_FINE_LOCATION" in your manifest?
also try changing ACCURACY_FINE to ACCURACY_HIGH
This is my first time development in android application. This app will calculate the total distance between two address that input by the user in order to get the total distance. Is it ok to calculate the distance that assume the road is straight? by the way, this application will not show map and does not need GPS! It just needs to use geocoder to get the latitude-longitude.
Here are my questions:
what would the AndroidManifest.xml would look like? do i need to include android:name=".permission.MAPS_RECEIVE".
In the main_activity.java, should i use activity or use fragment activity?
public class MainActivity extends Activity
Thanks for your reply and i would like to appreciate u guys and if possible can you please show the full AndroidManifext.xml code?
Thanks.
http://developer.android.com/reference/android/location/Location.html
Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
or
private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
float pk = (float) (180/3.14169);
float a1 = lat_a / pk;
float a2 = lng_a / pk;
float b1 = lat_b / pk;
float b2 = lng_b / pk;
float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
double tt = Math.acos(t1 + t2 + t3);
return 6366000*tt;
}
You don't need to include the maps permissions.
To get the distance between 2 locations you can use the Geocoder class. This shouldn't need an y location permissions either.
The Geocoder class has a method
getFromLocationName(String locationName, int maxResults)
which returns a list of Addresses for a location input. The addresses have a latitude/longitude.
You need to convert the Address into a Location object which has methods for calculating distance between 2 locations:
distanceTo(Location dest)
Returns the approximate distance in meters between this location and the given location.
As for your second question. You should use Fragments inside of your activity. Have a read through this to understand a bit more about fragments and their advantages.
Use the Geocoder to get the latitude and longitude for place 1 and place 2. Apply those values in the following method.
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results)
Let's say that I am collecting users location data every 20 minutes. At the end of the day I have an ArrayList of objects that contain lat and long fields and other data.
I have two problems that I am facing and trying to figure out:
Some of the locations are taken from inside of a building so they are not very accurate and could be spread around the actual location where the user was at the time.
Some of the locations are taken at different times but from the same location, as the user didn't moved.
What I want to achieve is to find all the locations that are near one another: lets say 70 meters, find the average location of all those locations and replace them only with this one average location.
So I am coming to the two important questions:
What would be the best way to find all near locations < 70 meter distance (Take in mind that the array contains valid changes in location. So I have to find the groups of near ones and leave the others intact).
Is there a method or a way to find the average location of many near ones?
Regarding near positions I previously answered a similar question here: Android Maps v2 - animate camera to include most markers
Specifically I think you would be able to use this piece of code:
private List<Marker> getSurroundingMarkers(List<Marker> markers,
LatLng origin, int maxDistanceMeters) {
List<Marker> surroundingMarkers = surroundingMarkers = new ArrayList<Marker>();
if (markers == null) return surroundingMarkers ;
for (Marker marker : markers) {
double dist = distBetween(origin, marker.getPosition());
if (dist < maxDistanceMeters) {
surroundingMarkers.add(marker);
}
}
return surroundingMarkers;
}
private float distBetween(LatLng pos1, LatLng pos2) {
return distBetween(pos1.latitude, pos1.longitude, pos2.latitude,
pos2.longitude);
}
/** distance in meters **/
private float distBetween(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
return (float) (dist * meterConversion);
}
Possibly rewriting the Marker part to use LatLng instead.
regarding the averaging, it should be a simple matter of (say you have n LatLng's):
lat_avg = (lat1+lat2+...+latn)/n
lng_avg = (lng1+lng2+...+lngn)/n
latlng_avg = new LatLng(lat_avg, lat_lng)
I' not sure how you're getting the user's location - whether your using a traditional LocationManager or play services. I've always modeled my location getting on this approach documented on the android developers blog. If you're flexible in switching between location gathering methods, whether the person is inside or outside should not matter that much. You should be getting the best possible location at any given time.
Assuming you're getting locations on a sufficient time schedule (I don't know when you're checking for updates but since you want everything inside a 70m radius I'm assuming its on a time schedule and not distance change) the basic way to find the average point is:
(1) Convert each lat/long pair into a unit-length 3D vector.
(2) Sum each of those vectors
(3) Normalise the resulting vector
(4) Convert back to spherical coordinates
That approach is documented here as well as in a much earlier SO post on calculating the average of a set of angles
The example code is pretty easy to follow - just plug in the lat long values you get from your location grab and you should be ok.
Well for markers that come from the same location I have created the following method:
public ArrayList<MyLocation> removeSameLocationMarkers(List<ParseObject> objects, int maxDistanceMeters)
{
boolean isLocationExist;
ArrayList<MyLocation> acceptedLocations = new ArrayList<MyLocation>();
if (objects == null) return acceptedLocations;
for (ParseObject location1 : objects)
{
isLocationExist = false;
for (MyLocation location2 : acceptedLocations)
{
if (!location1.equals(location2))
{
float distance = distBetween(location1.getDouble("latitude"), location1.getDouble("longitude"), location2.getLatitude(), location2.getLongitude());
if (distance < maxDistanceMeters)
{
location2.addTimeToLocation(location1.getString("time"));
isLocationExist = true;
}
}
}
if (!isLocationExist)
{
Location newLocation = new Location("");
newLocation.setLatitude(location1.getDouble("latitude"));
newLocation.setLongitude(location1.getDouble("longitude"));
String provider = location1.getString("provider");
if (provider != null)
{
newLocation.setProvider(provider);
}
MyLocation newMyLocation = new MyLocation(newLocation);
newMyLocation.addTimeToLocation(location1.getString("time"));
acceptedLocations.add(newMyLocation);
}
}
return acceptedLocations;
}