Launching Google Maps Directions via an intent on Android

后端 未结 15 1426
小鲜肉
小鲜肉 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

    Using the latest cross-platform Google Maps URLs: Even if google maps app is missing it will open in browser

    Example https://www.google.com/maps/dir/?api=1&origin=81.23444,67.0000&destination=80.252059,13.060604

    Uri.Builder builder = new Uri.Builder();
    builder.scheme("https")
        .authority("www.google.com")
        .appendPath("maps")
        .appendPath("dir")
        .appendPath("")
        .appendQueryParameter("api", "1")
        .appendQueryParameter("destination", 80.00023 + "," + 13.0783);
    String url = builder.build().toString();
    Log.d("Directions", url);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
    
    0 讨论(0)
  • 2020-11-22 04:37

    Open Google Maps using Intent with different Modes:

    We can open Google Maps app using intent:

    val gmmIntentUri = Uri.parse("google.navigation:q="+destintationLatitude+","+destintationLongitude + "&mode=b")
    val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri)
    mapIntent.setPackage("com.google.android.apps.maps")
    startActivity(mapIntent)
    

    Here, "mode=b" is for bicycle.

    We can set driving, walking, and bicycling mode by using:

    • d for driving
    • w for walking
    • b for bicycling

    You can find more about intent with google maps here.

    Note: If there is no route for the bicycle/car/walk then it will show you "Can't find the way there"

    You can check my original answer here.

    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题