-3

I want to use google map api in android studio to find distance between 2 places for some computation purpose and but i dont want to display the map or the markers ,how do i do that?

  • 1
    Possible duplicate of [Find distance between two points on map using Google Map API V2](https://stackoverflow.com/questions/14394366/find-distance-between-two-points-on-map-using-google-map-api-v2) – person Sep 12 '18 at 15:43

3 Answers3

1

If all you want is to calculate the distance between two points on the earth and you have the coordinates in latitude and longitude then there is no need for any Google API's Maps or libraries. That just costs extra overhead and maintenance. Just make a static method like this:

public static double getDistanceMeters(LatLng pt1, LatLng pt2){
    double distance = 0d;
    try{
        double theta = pt1.longitude - pt2.longitude;
        double dist = Math.sin(Math.toRadians(pt1.latitude)) * Math.sin(Math.toRadians(pt2.latitude))
                + Math.cos(Math.toRadians(pt1.latitude)) * Math.cos(Math.toRadians(pt2.latitude)) * Math.cos(Math.toRadians(theta));

        dist = Math.acos(dist);
        dist = Math.toDegrees(dist);
        distance = dist * 60 * 1853.1596;
    }
    catch (Exception ex){
        System.out.println(ex.getMessage());
    }
    return distance;
} 

Same algorithm will work for any platform. Just translate it to the appropriate program language.

Barns
  • 4,850
  • 3
  • 17
  • 31
0

You can refer this post to find distance between 2 places.

But if you don't want to show the map or markers you simply put the height and width of map fragment as 0dp.

Here is an example:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:context="com.example.ahmedsamra.mansouratourguideapp.MapsActivity" />
Anubhav Gupta
  • 2,492
  • 14
  • 27
0

If you want to calculate distance between two places without show a Google Map, I think that instead of Google Map, you must implement Distance Matrix API

I show you an example taken from the documentation

request:

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=Washington,DC&destinations=New+York+City,NY&key=YOUR_API_KEY

JSON response:

{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "Washington, DC, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "225 mi",
                  "value" : 361715
               },
               "duration" : {
                  "text" : "3 hours 49 mins",
                  "value" : 13725
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Or use the method disntanceTo() of Location that returns shortest distance between two points

locationA.setLatitude(aLatA);
locationA.setLongitude(aLnA);

locationB.setLatitude(aLatB;
locationB.setLongitude(aLngB);

distanceBetweenLocations = locationA.distanceTo(locationB) 
Julio Lemus
  • 651
  • 4
  • 8