zookastos
zookastos

Reputation: 945

How to get a callback from google maps in Android application, when navigation is complete

I am building an android application, that will show certain points on google maps, for which I have (lat,lang) for all the points shown, with me. Whenever, a user clicks on one of these locations on google maps, I am starting a new Google Maps intent which shows navigation to the user and is supposed to take the user to that (lat, lang).

//suppose this function is called on onclick of google maps event
    function onclick(){
            Uri gmmIntentUri = Uri.parse("google.navigation:q=32.885240,-96.763475");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            if (mapIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(mapIntent);
            }
    }

This will start the navigation for the user. Now what I want to achieve is this - Whenever this navigation is complete, I want some kind of flag to be passed to my android application, so that my application knows that the navigation has been finished.

If it is possible to automatically close the google maps application after the navigation has been completed, it will be nice to have.

Any help is greatly appreciated. Thanks. Anyone has any solutions for this?

Upvotes: 3

Views: 3062

Answers (1)

AniV
AniV

Reputation: 4037

You need to call the finish() method on the activity when the destination is reached. So here are the steps that you need to implement in your program.

  • First of all know the destination coordinates from the user input and pass it in the Direction API.

    https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=API_KEY
    
  • When you know the destination coordinates you can pass that when the marker representing that is clicked in the navigation intent. (which you have already done)

  • Make use of the location listener interface that tracks the current location coordinates.

  • Put an if...else condition where you check that the current location is equivalent to the destination.

  • When the current location == Destination make a call to activity.finish() which will finally close the maps activity hosting.

Hope that Helps!!

Upvotes: 1

Related Questions