Prince
Prince

Reputation: 9

Android | Get North/S/E/W lat & long range from current position

I am doing a project on an application where by I need to discover other devices based on my current location.

These devices I want to filter in a way whereby they are classified by North/South/East/West of my device pointing direction.

I read some article saying converting compass bearing into lat & long but how do I know the lat & long is to my right or left ? north or south ?

Upvotes: 0

Views: 2050

Answers (2)

Sam
Sam

Reputation: 3155

So I am assuming you have Latitude and Longitude coordinates. Latitude denotes North and South of the equator and longitude denotes East and West of the Prime Meridian.

If you want to convert latitutde-longitude to East-West-North-South direction you can use this scheme.

Positive latitude means North of the equator, and Positive longitude means East of the Prime Meridian and vice versa.

So in short

+,+ -> North/East
+,- -> North/West
-,+ -> South/East
-,- -> South/West

Using this key you can find out relative direction of any lat-lon pair from your base location.

Edit Example

If your location is (50,50) and some other device has location (60,60) that means the other device is 10 degrees North and 10 degrees East to your relative location. You can easily implement this logic in android.

Upvotes: 0

Sander
Sander

Reputation: 816

The Android Maps Util library has a function to calculate the bearing between 2 LatLng Objects.

Include the Android Maps library if you don't have it already

implementation 'com.google.maps.android:android-maps-utils:0.5'

Compare your current LatLng with the LatLng of the other device.

LatLng myLocation;
LatLng otherPhone;

double heading = SphericalUtil.computeHeading(myLocation, otherPhone);

Now you can use the heading however you want, the double represents the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).

Documentation can be found here

Upvotes: 1

Related Questions