4

I have been working on Google maps services in Android. I want to know if there is a utility method that gives the direction of a LatLng in respect to another one.

I want to know if a point is in

  • South or north

  • East or west

  • OR a combination of two, one from each

SomeUtil.direction(latlng1,latlng2) -> give me the direction of latlng2 with respect to latlng1 for example south west

Thanks in advance

1
  • Android's Location class has a bearingTo() method to calculate the angle and it should be easy to convert from LatLng to Location. 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. Commented Feb 8, 2018 at 14:47

2 Answers 2

6

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

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.

1

You can use the computeOffset method from the Google Maps Android API Utility Library (https://developers.google.com/maps/documentation/android-api/utility/):

Look at the answer in this question

public static LatLng computeOffset(LatLng from, double distance, double heading)

Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).

Parameters:

  • distance - The distance to travel.
  • from - The LatLng from which to start.
  • heading - The heading in degrees clockwise from north.

You have the distance and all the LatLng. You can pass the angle values accordingly and check whether they satisfy your equation or not.

3
  • Thanks for your answer but this utility method seems to be doing something in reverse. I do not want to provide a distance or a heading value. I rather want to provide two points and find heading value in this example
    – ikbal
    Commented Feb 8, 2018 at 13:30
  • why negative vote? I mentioned, you can out values in heading to check whether it matches the resultant LatLng. Commented Feb 8, 2018 at 15:03
  • Hey mate I haven't voted down your answer. Somebody else must have done
    – ikbal
    Commented Feb 8, 2018 at 15:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.