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

荒凉一梦 提交于 2019-12-23 16:16:07

问题


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?


回答1:


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!!



来源:https://stackoverflow.com/questions/31443532/how-to-get-a-callback-from-google-maps-in-android-application-when-navigation-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!