简体   繁体   中英

open google maps through intent for specific location in android

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 . Following is my Intent code..

String url = "http://maps.google.com/maps?daddr="+address;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url));
startActivity(intent);

But it gives me Google Map for getting direction. I know why that so, because I used daddr in url but I don't know what to use for specific location..Please tell me what to use there..

I have not tested this but you could try :

First method:

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

EDIT: This might not work with Google maps 7,0

hence you could change the uri to :

Second option:

String geoUri = "http://maps.google.com/maps?q=loc:" + lat + "," + lng + " (" + mTitle + ")";

where mTitle is the name of the location.

Third option:

geo:0,0?q=my+street+address

Fourth option:

String map = "http://maps.google.co.in/maps?q=" + yourAddress;

Hope that works and helps :D..

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.

The latest version of map provides better solution. If you wish show an address on map use below code.

Uri mapUri = Uri.parse("geo:0,0?q=" + Uri.encode(address));
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

If you want to display using latitude and longitude values, use below method.

Uri mapUri = Uri.parse("geo:0,0?q=lat,lng(label)");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, mapUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

Lat and lng being the latitude and longitude you wish to display, the label here is optional.

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();
    }

This will work like a charm. Make sure to check for existence for Google Maps, so this can work universally across all non Google devices also. It will open in a browser in such cases.

Also, remember, don't have www in the URL. Else this will not work.

        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?q=loc:" + latitude + "," + longitude));
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Only if initiating from a Broadcast Receiver
        String mapsPackageName = "com.google.android.apps.maps";
        if (isPackageExisted(context, mapsPackageName)) {
            i.setClassName(mapsPackageName, "com.google.android.maps.MapsActivity");
            i.setPackage(mapsPackageName);
        }
        context.startActivity(i);

private static boolean isPackageExisted(Context context, String targetPackage){
    PackageManager pm=context.getPackageManager();
    try {
        PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
    return true;
}

As of 2020 you can also consider using Google Maps URLs , the API designed by Google in order to create universal cross-platform links. You can open Google maps on web, Android or iOS using the same URL string in form:

https://www.google.com/maps/search/?api=1&parameters

In case when you need show certain location you can use an URL like

https://www.google.com/maps/search/?api=1&query=36.26577,-92.54324

The code snippet is

String url = "https://www.google.com/maps/search/?api=1&query="+address;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);

I hope this helps!

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

  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();
                }
            }
        });

This helper method using uriBuilder for cleaner code and handle condition if there is no activity on device that can open map

public static boolean openMap(Context context, String address) {
    Uri.Builder uriBuilder = new Uri.Builder()
            .scheme("geo")
            .path("0,0")
            .appendQueryParameter("q", address);
    Intent intent = new Intent(Intent.ACTION_VIEW, uriBuilder.build());
    if (intent.resolveActivity(context.getPackageManager()) != null) {
        context.startActivity(intent);
        return true;
    }
    return false;
}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address)); startActivity(intent);

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.

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);

I find this solution in google documentation so, it will work fine.

Double latitude = 37.7749,longitude = -122.4192;
            String label = "Your location desired Aryan";
            Uri gmmIntentUri =
                    Uri.parse("geo:0,0?q="+latitude+","+longitude+"("+label+")");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            context.startActivity(mapIntent);

If you have any doubt just comment it. I am not checking maps package because on every android device we have maps. If you want you check it easily. If u found it helpful please upvote.

String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%f,%f", recentBean.getLatitude(), recentBean.getLongitude());
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
            mContext.startActivity(intent);

https://www.google.com/maps/dir//37.4219983,-122.084

String location = " https://www.google.com/maps/dir// " + latitude + "," + longitude; try { SmsManager sms = SmsManager.getDefault(); // using android SmsManager sms.sendTextMessage(number, null, message + location, null, null); // adding number and text Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(this, "Sms not send, please check phone number/network", Toast.LENGTH_SHORT).show(); e.printStackTrace(); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM