How to calculate distance from the user’s location to another address?

Asked

Viewed 5,153 times

2

What I need to do is very simple, but I don’t know how to do it.

It works like this: I pass an address to the user and once he is in place he presses a button, while doing this the application must know if he is in place (or very close), if he is, executes a command, otherwise informs the user that he is far from the location. How can I do that?

I’m using Android Studio.

  • 1

    You just want to know how to calculate the distance between two coordinates or you want to know how to do everything that talks there in the question?

  • 1

    And what have you done so far? Are you using any API or are you using native GPS? Could you give details?

  • 1

    So, ramaral, I need to know if the distance between the two coordinates is close, to validate if the user is in place. It’s like this: XYZ event on Unnamed Street, n° 10, I want to get the user’s coordinate and know if he’s on site. Guilherme, until now I only have the interface (mounted through Mysql data) just need to implement this function in the existing button, but as I am new in mobile development, I have no idea how to start hahaha I made the entire application using tutorials.

  • Well, I was able to obtain user longitude and latitude (-22.75678733; -42.88711433) through this link: http://stackoverflow.com/questions/6567171/how-to-get-users-gps-coordinatdinates-by-clicking-a-button and now how do I know the distance between that coordinate and any other? Instead of longitude and latitude as in the link above, you can capture an address (street name and number )?

  • You do not need to keep editing the posts and marking them as solved. Just accept the answer. Please read the "TOUR" page of the site

2 answers

6


You can use Google’s own lib for this. Google Maps Android API Utility Library

Add to your Gradle:

dependencies {
    compile 'com.google.maps.android:android-maps-utils:x.y.z'
}

example:

LatLng posicaoInicial = new LatLng(latitude,longitude);
LatLng posicaiFinal = new LatLng(latitude,longitude);

double distance = SphericalUtil.computeDistanceBetween(posicaoInicial, posicaiFinal);
    Log.i("LOG","A Distancia é = "+formatNumber(distance));

private String formatNumber(double distance) {
        String unit = "m";
        if (distance  1000) {
            distance /= 1000;
            unit = "km";
        }

        return String.format("%4.3f%s", distance, unit);
    }

`

  • Failed to find com.google.maps.android:android-maps-utils:x.y.z I already traded x.y.z for 0.3+, 0.3.1, 0.4+ but it won’t.. What’s missing? I think it’s a missing library, but on sdk manager I couldn’t find anything to download

  • 1

    x.y.z This means you have to get the latest lib update, namely: Compile 'com.google.maps.android:android-maps-utils:0.4+

  • 1

    Thanks, man, it worked! the/

3

If you want to know the distance of the route, you can use the Google Maps Distance Matrix API.

This API returns for example the time and duration between points.

Example: Distance between SP - RJ

Using your own coordinates.

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=-23.562984,-46.654261&destinations=-22.951669,-43.210466&key=KEY_CODE

Return:

{
   "destination_addresses" : [ "Christ the Redeemer Statue Rio, Rio de Janeiro - RJ, Brasil" ],
   "origin_addresses" : [
      "Alameda Rio Claro, 313 - Bela Vista, São Paulo - SP, 01332-010, Brasil"
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "449 km",
                  "value" : 449025
               },
               "duration" : {
                  "text" : "5 horas 45 minutos",
                  "value" : 20689
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Result: 5h 45min - 449 km

Browser other questions tagged

You are not signed in. Login or sign up in order to post.