ikbal
ikbal

Reputation: 1894

Direction of a LatLng in respect to another one

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

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

Thanks in advance

Upvotes: 4

Views: 2001

Answers (2)

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13353

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.

Upvotes: 6

karandeep singh
karandeep singh

Reputation: 2334

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.

Upvotes: 1

Related Questions