How to open standard Google Map application from my application?

后端 未结 9 865
星月不相逢
星月不相逢 2020-11-28 01:09

Once user presses button in my application, I would like to open standard Google Map application and to show particular location. How can I do it? (without using com.g

相关标签:
9条回答
  • 2020-11-28 01:56

    You should create an Intent object with a geo-URI:

    String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    context.startActivity(intent);
    

    If you want to specify an address, you should use another form of geo-URI: geo:0,0?q=address.

    reference : https://developer.android.com/guide/components/intents-common.html#Maps

    0 讨论(0)
  • 2020-11-28 02:01

    To go to location WITH PIN on it, use:

    String uri = "http://maps.google.com/maps?q=loc:" + destinationLatitude + "," + destinationLongitude;
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    intent.setPackage("com.google.android.apps.maps");
    startActivity(intent);
    

    for without pin, use this in uri:

     String uri = "geo:" + destinationLatitude + "," + destinationLongitude;
    
    0 讨论(0)
  • 2020-11-28 02:06

    You can also use the code snippet below, with this manner the existence of google maps is checked before the intent is started.

    Uri gmmIntentUri = Uri.parse(String.format(Locale.ENGLISH,"geo:%f,%f", latitude, longitude));
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    if (mapIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(mapIntent);
    }
    

    Reference: https://developers.google.com/maps/documentation/android-api/intents

    0 讨论(0)
提交回复
热议问题