NORTH, SOUTH, EAST, WEST and its combinations like NORTH-EAST is just names of heading intervals, which (heading) you can get from computeHeading()
method of SphericalUtil
class in Google Maps Android API Utility Library:
computeHeading
public static double computeHeading(LatLng from, LatLng to)
Returns the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).
Returns: The heading in degrees clockwise from north.
and than get direction by converting heading compass degrees to direction names (Δ, delta - "base" angle for determine direction = 22.5 degrees because of 360 / 8 / 2, where^ 8 - number of directions north, south etc.):
![Directions](https://cdn.statically.io/img/i.sstatic.net/CNaVX.png)
Full source code:
enum LocationDirection {
UNKNOWN,
NORTH,
NORTH_EAST,
EAST,
SOUTH_EAST,
SOUTH,
SOUTH_WEST,
WEST,
NORTH_WEST
}
public static LocationDirection direction(LatLng latlng1, LatLng latlng2) {
double delta = 22.5;
LocationDirection direction = LocationDirection.UNKNOWN;
double heading = SphericalUtil.computeHeading(latlng1, latlng2);
if ((heading >= 0 && heading < delta) || (heading < 0 && heading >= -delta)) {
direction = LocationDirection.NORTH;
} else if (heading >= delta && heading < 90 - delta) {
direction = LocationDirection.NORTH_EAST;
} else if (heading >= 90 - delta && heading < 90 + delta) {
direction = LocationDirection.EAST;
} else if (heading >= 90 + delta && heading < 180 - delta) {
direction = LocationDirection.SOUTH_EAST;
} else if (heading >= 180 - delta || heading <= -180 + delta) {
direction = LocationDirection.SOUTH;
} else if (heading >= -180 + delta && heading < -90 - delta) {
direction = LocationDirection.SOUTH_WEST;
} else if (heading >= -90 - delta && heading < -90 + delta) {
direction = LocationDirection.WEST;
} else if (heading >= -90 + delta && heading < -delta) {
direction = LocationDirection.NORTH_WEST;
}
return direction;
}
NB! You need to add line
compile 'com.google.maps.android:android-maps-utils:0.5+'
to dependencies
section of build.gradle
file for SphericalUtil
class using.
bearingTo()
method to calculate the angle and it should be easy to convert fromLatLng
toLocation
. But maybe the Google Maps Android API Utility Library has what you need. I'm not sure actually, so this is just a comment instead of a proper answer.