Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmytrodanylyk/1bc99cbb57343379d871 to your computer and use it in GitHub Desktop.
Save dmytrodanylyk/1bc99cbb57343379d871 to your computer and use it in GitHub Desktop.
Android Locations
private static final int MIN_STEP_DISTANCE = 10; // meter
public List<LatLng> createIntermediatePoints(@NotNull LatLng start, @NotNull LatLng end) {
List<LatLng> pointList = new ArrayList<LatLng>();
double startLat = start.latitude;
double startLon = start.longitude;
double endLat = end.latitude;
double endLon = end.longitude;
float distance = LocationUtils.distance(start, end);
int totalSegments = (int) (distance / MIN_STEP_DISTANCE);
for (int segment = 1; segment <= totalSegments; segment++) {
double lat = (startLat + ((endLat - startLat) / (double) totalSegments) * segment);
double lon = (startLon + ((endLon - startLon) / (double) totalSegments) * segment);
pointList.add(new LatLng(lat, lon));
}
return pointList;
}
public class LocationUtils {
public static float distance(@NotNull Location from, @NotNull Location to) {
return distance(from.getLatitude(), from.getLongitude(), to.getLatitude(), to.getLongitude());
}
public static float distance(@NotNull LatLng from, @NotNull LatLng to) {
return distance(from.latitude, from.longitude, to.latitude, to.longitude);
}
public static float distance(double fromLat, double fromLon, double toLat, double toLom) {
float distance = 0;
float[] result = new float[3];
Location.distanceBetween(fromLat, fromLon, toLat, toLom, result);
if (result.length > 1) {
distance = result[0];
}
return distance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment