open google maps through intent for specific location in android

后端 未结 12 1082
别那么骄傲
别那么骄傲 2020-11-30 20:17

I\'m designing one application in which I want to show specific location on Map. I\'m passing String of address which is already placed on Google Map

相关标签:
12条回答
  • 2020-11-30 20:42

    You should use something like this

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
        Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
    startActivity(intent);
    

    And to drop a pin

    try {
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
            Uri.parse("geo:" + AppointmentDetailLayout.docLatitude
                + "," + AppointmentDetailLayout.docLongitude
                + "?q=" + AppointmentDetailLayout.docLatitude
                + "," + AppointmentDetailLayout.docLongitude
                + "(" + label + ")"));
        intent.setComponent(new ComponentName(
            "com.google.android.apps.maps",
            "com.google.android.maps.MapsActivity"));
        context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
    
        try {
            context.startActivity(new Intent(
                Intent.ACTION_VIEW,
                Uri.parse("market://details?id=com.google.android.apps.maps")));
        } catch (android.content.ActivityNotFoundException anfe) {
            context.startActivity(new Intent(
                Intent.ACTION_VIEW,
                Uri.parse("http://play.google.com/store/apps/details?id=com.google.android.apps.maps")));
        }
    
        e.printStackTrace();
        }
    
    0 讨论(0)
  • 2020-11-30 20:42

    For the ones, who are looking for opening the "Maps" app for a particular location(by passing lat and long) and do not want that lat/long co-ordinates to be shown in Maps search bar after redirection(Opening Maps app). Instead wish to show some label or place name, you can refer to the code below,

    private void openMapView(String latitude , String longitude , String locationName){
        Uri gmmIntentUri = Uri.parse("geo:"+latitude+","+longitude+"?q="+locationName);
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(mapIntent);
        }
    }
    

    Note : "locationName" in the method above, represents the name you wish to display inside the Maps search bar.

    0 讨论(0)
  • 2020-11-30 20:44

    To show location and disply show directions button inside google Maps use this code snippet:

    String geoUri = "http://maps.google.com/maps?q=loc:" + latitude + "," + longitude + " (" + locationName + ")";
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
    if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(mapIntent);
    
    0 讨论(0)
  • 2020-11-30 20:47

    Get Lat-Lng Using this web-service

    http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false

    Then Pass it to this code

        String strUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + "Label which you want" + ")";
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(strUri));
    
        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    
        startActivity(intent);
    

    I hope it will help you

    Thank you.

    0 讨论(0)
  • 2020-11-30 20:50
    Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    if (mapIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(mapIntent);
    }
    

    Refer this documentation

    0 讨论(0)
  • 2020-11-30 20:50
      try {
                        Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                                Uri.parse("http://maps.google.com/maps?saddr=" + src_lat+ "," + src_lng + "&daddr=" + des_lat + "," + des_lng));
                        startActivity(intent);
                    }catch (ActivityNotFoundException  ane){
    
                        Toast.makeText(activity, "Please Install Google Maps ", Toast.LENGTH_LONG).show();
                    }catch (Exception ex){
                        ex.getMessage();
                    }
                }
            });
    
    0 讨论(0)
提交回复
热议问题