open google maps through intent for specific location in android
Asked Answered
H

15

70

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..

Heptarchy answered 28/3, 2014 at 4:50 Comment(2)
hi I am developing a trip planner project, is completely free to use 'open google maps through intent for a specific location in android', or any pricing if reached quote?? do you know anything about this plz shareChadbourne
Its completely free to open specific location via intent as you are opening it in external App and not in yours..Heptarchy
E
156

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..

Ethmoid answered 28/3, 2014 at 5:2 Comment(9)
and what about lat and lng ? I don't want to pass them, I'm just passing addressHeptarchy
oh alright, il have to google that part :).. like i said iv not tried this either :).. il get backEthmoid
Please try, and if you get something the tell meHeptarchy
but i think i did read it somewhere that it would ignore the lat and long and simply take the location denoted by mTitleEthmoid
and you could catch ur ears the other way round, get lat and long from the address and then pass those to the string uriEthmoid
@Aksh hi, try geo:0,0?q=my+street+addressEthmoid
Thanks, your fourth solution worked so easily, just have to add q ..:)Heptarchy
Second one is mine. I used it without adding title and it works.Cheese
Any idea how to pass the adress to the search bar of Google Maps?Logway
H
40

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.

Hooten answered 28/3, 2014 at 5:5 Comment(0)
D
39

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.

Diathermy answered 12/9, 2016 at 6:46 Comment(2)
+1, this is the only option that doesn't explicitly mention Google Maps. The ?q=lat,lon parameter has better results too, as it displays a pin on the opened map (the first lat,lon only pans the map to the location). The setPackage call is optional.Cupcake
FYI, Google Maps is not displaying the value for "label" when the 2nd option you've shown is used. I have verified that regionData.name is a String in the following code snippet Uri.parse("geo:0,0?q=loc:${regionData.location.latitude},${regionData.location.longitude} (${regionData.name})")Chunchung
D
8

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;
}
Dear answered 3/1, 2015 at 5:4 Comment(0)
D
5

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);
Differentia answered 25/1, 2020 at 22:19 Comment(0)
F
3
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address));
startActivity(intent);
Fung answered 26/2, 2021 at 17:15 Comment(0)
Y
2
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

Yellow answered 21/9, 2016 at 14:21 Comment(0)
I
2
  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();
                }
            }
        });
Incondensable answered 27/9, 2017 at 12:30 Comment(0)
A
1

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;
}
Accrete answered 14/6, 2017 at 13:35 Comment(0)
K
1

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);
Kinkajou answered 4/3, 2020 at 12:58 Comment(0)
A
1

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.

Angelinaangeline answered 3/7, 2022 at 5:36 Comment(0)
F
1

I thought I would chip in with the Kotlin code.

I created a helper function that takes latitude, longitude and context params to fire off the intent.

fun openGoogleMap(latitude: Double, longitude: Double, context: Context) { ... }

Inside the openGoogleMap() helper function create an intent passing in the action, data to act upon (lat/long) and package name for the Google Map application.

 fun openGoogleMap(latitude: Double, longitude: Double, context: Context) {
    
        // you need action, data and package name
    
        val uri = Uri.parse("geo:$latitude,$longitude?z=15")
        //create an intent
        val mapIntent = Intent()
    
        //add action & data
        mapIntent.action = Intent.ACTION_VIEW
        mapIntent.data = uri
    
        //package name for google maps app
        mapIntent.setPackage("com.google.android.apps.maps")
        try {
            context.startActivity(mapIntent)
        } catch (e: ActivityNotFoundException) {
    
            //if map app isn't resolved/installed catch error
            e.printStackTrace()
        }

}

The docs say that

To verify that an app is available to receive the intent, call resolveActivity() on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity().

But I found that using resolveActivity() has issues when used with this code especially when you are outside the Activity's code.

mapIntent.resolveActivity(packageManager)?.let {
  ...
}

To get around this I used try-catch block to catch ActivityNotFoundException with startActivity().

mapIntent.setPackage("com.google.android.apps.maps")
        try {
            context.startActivity(mapIntent)
        } catch (e: ActivityNotFoundException) {

            //if map app isn't resolved/installed catch error
            e.printStackTrace()
        }

I tested the util function and it does work and is launching the GoogleMaps with Kotlin code.

Fainthearted answered 6/12, 2022 at 11:20 Comment(0)
D
0

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.

Deutero answered 24/1, 2020 at 7:14 Comment(0)
S
0

If you have the name and address of the location available then you can launch the exact spot along with its images, details and navigation option using the following code :

    fun loadLocation(ctx: Context, name: String, address: String) {
        try {
            val geoUri = "http://maps.google.com/maps?q=$name,$address"
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(geoUri))
            ctx.startActivity(intent)
        }
        catch (ex: Exception) {
            ex.printStackTrace()
        }
    }
Sanctitude answered 8/2, 2023 at 14:23 Comment(0)
J
-2

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();
}
Jubilate answered 13/8, 2019 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.