I am developing an application that shows the distance between the user and multiple locations (such as foursquare in the image below) on android, I am using eclipse and would like to know how to calculate the distance between various points. Thank you!
Image:
http://i.stack.imgur.com/rsWmO.jpg
There are probably many ways to get this done, here's an option.
You could use the distanceTo() method. If you want more than one distance, simply use a loop to repeat it until you've calculated the distances between all the Locations you have at hand.
If you're using Google Maps, you can use a Distance Matrix
https://developers.google.com/maps/documentation/distancematrix/
https://developers.google.com/maps/documentation/javascript/distancematrix
Here I am providing you some sample code for Distance calculation. I have done like this in my project. Here distanceTo() method will return you the distance in double.
private Location currentLocation, distanceLocation;
double distance = 0;
//set your current location
currentLocation.setLatitude(currentLat);
currentLocation.setLongitude(currentLong);
//set your destination location
distanceLocation = new Location("");
distanceLocation.setLatitude(destinatioLat);
distanceLocation.setLongitude(destinationLong);
distance = currentLocation.distanceTo(distanceLocation)/1000;
Same for more then one location you can use Array for storing distance. Its on you how you want to use it as per your requirement.
Hope this will help you.
Related
I'm developing an Android app that tracks the users location. I have it running locally so my LatLng are getting stored in an array :
ArrayList<LatLng> points = new ArrayList<>();
In onLocationChanged it then uses this to draw a poly line, this all works perfect for me. I want to calculate the distance of the entire journey. Is there a way to do this using my array?
You could make a loop that go through your array and compute the distance between two consecutive points. Then add every distance computed to obtain the whole trip distance. This should look like this (in pseudo code) :
totalDistance;
for(points in listOfPoints){
nextPoint = listOfPoints.indexof(point + 1);
distance = computeDistance(point, nextPoint);
totalDistance.add(distance);
}
For calculation distance between location points, you need at least two points.
Calculation can be found here:
https://stackoverflow.com/a/365853/1537916
My app gets updated GPS coordinates periodically which I show using a Marker on map. I need to move the marker to a new position if the new GPS coordinates are different then what Marker is currently showing.
The problem is that comparing Marker.getPosition() is more accurate while LatLang is not, hence sometimes even when they are the same, my logic says they are different.
How to solve this issue?
Please note that the same LatLang i assign to Marker.
You can consider that two LatLngs are virtually the same if the distance between them is less than a given tolerance.
You can use the SphericalUtil.computeDistanceBetween method from the Google Maps Android API Utility Library
float YOUR_TOLERANCE = 1; // 1 meter
if (SphericalUtil.computeDistanceBetween(pos, buslatLng) < YOUR_TOLERANCE) {
// Both locations are considered the same
}
What would be the best way to know the nearest place of the list against the current user location.
is there a way to calculate the difference between two geo points in maps?
You can get the distance between 2 points making 2 Location objects (one for each point) and calling the method distanceTo.
Example.
// Instantiate.
Location pointOne=new Location("");
Location pointTwo=new Location("");
// Configure.
pointOne.setLatitude(doubleValue);
pointOne.setLongitude(doubleValue);
pointTwo.setLatitude(doubleValue);
pointTwo.setLongitude(doubleValue);
// Calculate distance.
float distanceInMeters=pointOne.distanceTo(pointTwo);
And that's all.
By the way, watch your grammar for better readibility =)
Good luck.
How to find distance from polyline in google map in android using map apiV2 ? I want to find distance when user start moving from one location to another .
I used the Calculate distance in meters when you know longitude and latitude in java but it takes distance between two lat-long not as per user
moves in map. Please help me if anyone knows how to find distance using polyline.
For finding distance from your polyline you can use PolyUtil class which contains one method distanceToLine .
implementation'com.google.maps.android:android-maps-utils:0.5+'
add this library in your build.gradle.
For more you can use Google map utility class
https://developers.google.com/maps/documentation/android-api/utility/
For this you need to use thread. In that at regular interval you should find the distance between current coordinates and last saved coordinate.
Raw code:
Coordinate current, last;
Distance D = 0;
while (true) {
wait(10000);
last = current;
current = getNewCordinate;
d = D + distFrom(current, last);
}
This logic will help.
Based on the answer given by Gevaria Purva I found a more appropriate method on PolyUtil named isLocationOnPath.
For the third parameter most likely you want to pass true.
I made a android application with multiple Geolocation on Google map (pins)
I would like to send a notification when the user is close to one of these locations.
Any ideas?
This is actually fairly easy. First, set up an application that monitors your location. This post will show you more on that.
Once you know your position, you can simply determine if you're within a certain range of it. To calculate the distance between two points, try the Location class:
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
results is a float[], so to return the distance, simply use float distance = results[0];.
So, in a nutshell, compile a list of waypoints you want to recognize. Then, in your GPS monitoring code, regularly check the distance between yourself and the list of points. If you're within a threshold, say 100m, then send a notification as a Toast message or something.
Good luck!