summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/scaleview/DistanceUtils.java
blob: 9a3b2da41526905cfc46dfda5d8b3d038fa9fae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.mapbox.mapboxsdk.maps.widgets.scaleview;


import com.mapbox.mapboxsdk.geometry.LatLng;

public class DistanceUtils {

    public static final double EARTH = 6371.137; //Radius of the Earth in km

    public static LatLng translatePoint(LatLng point, double distance, double bearing) {
        distance = distance / 1000d;

        //converts the Latitude, Longitude and bearings into radians
        double lat = Math.toRadians(point.getLatitude());
        double lng = Math.toRadians(point.getLongitude());
        bearing = Math.toRadians(bearing);

        //Give the distance and the first Latitude, computes the second latitude
        double Lat2 = Math.asin((Math.sin(lat) * Math.cos(distance / EARTH)) +
                (Math.cos(lat) * Math.sin(distance / EARTH) * Math.cos(bearing)));

        //Give the distance and the first longitude, computes the second longitude
        double Long2 = lng + Math.atan2(Math.sin(bearing) * Math.sin(distance / EARTH) * Math.cos(lat),
                Math.cos(distance / EARTH) - (Math.sin(lat) * Math.sin(Lat2)));

        //Converting the new Latitude and Longitude from radians to degrees
        Lat2 = Math.toDegrees(Lat2);
        Long2 = Math.toDegrees(Long2);

        //Creates a point object to return back. X is the longitude, Y is the Latitude
        return new LatLng(Lat2, Long2);
    }

}