0

I have a problen

Our developer ios has coded this function as follows

    let topLeft = mapView.projection.coordinate(for: mapView.bounds.origin)
    let bottomLeft = mapView.projection.coordinate(for: .init(x: mapView.bounds.minX, y: mapView.bounds.maxY))
    let distance = GMSGeometryDistance(topLeft, bottomLeft)

This is the introduction of function GMSGeometryDistance in iOs

 /**
* Returns the great circle distance between two coordinates, in meters, on Earth.
*
* This is the shortest distance between the two coordinates on the sphere.
*
* Both coordinates must be valid.
*/
 FOUNDATION_EXPORT
 CLLocationDistance GMSGeometryDistance(CLLocationCoordinate2D from, CLLocationCoordinate2D to);

I want to do this on google maps of android.

HOW TO CONVERT TO JAVA OR KOTLIN IN ANDROID.

Thanks you.

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22

1 Answers1

1

From Official Documentation of Maps SDK for iOS:

CLLocationDistance GMSGeometryDistance(CLLocationCoordinate2D from, CLLocationCoordinate2D to)

Returns the great circle distance between two coordinates, in meters, on Earth. This is the shortest distance between the two coordinates on the sphere. Both coordinates must be valid.

So, you can:

1) implement in manually follow description on Wiki (or other resources like geeksforgeeks) like in this answer of Usman Kurd:

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);

    return Radius * c;
}

2) use SphericalUtil.computeDistanceBetween(latLngFrom, latLngTo) from Maps SDK for Android Utility Library like in that answer of Björn Kechel.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79