Launching Google Maps Directions via an intent on Android

后端 未结 15 1432
小鲜肉
小鲜肉 2020-11-22 03:46

My app needs to show Google Maps directions from A to B, but I don\'t want to put the Google Maps into my application - instead, I want to launch it using an Intent. Is this

15条回答
  •  遥遥无期
    2020-11-22 04:37

    First you need to now that you can use the implicit intent, android documentation provide us with a very detailed common intents for implementing the map intent you need to create a new intent with two parameters

    • Action
    • Uri

    For action we can use Intent.ACTION_VIEW and for Uri we should Build it ,below i attached a sample code to create,build,start the activity.

     String addressString = "1600 Amphitheatre Parkway, CA";
    
        /*
        Build the uri 
         */
        Uri.Builder builder = new Uri.Builder();
        builder.scheme("geo")
                .path("0,0")
                .query(addressString);
        Uri addressUri = builder.build();
        /*
        Intent to open the map
         */
        Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);
    
        /*
        verify if the devise can launch the map intent
         */
        if (intent.resolveActivity(getPackageManager()) != null) {
           /*
           launch the intent
            */
            startActivity(intent);
        }
    

提交回复
热议问题