open google maps through intent for specific locat

2019-01-08 07:40发布

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

8条回答
看我几分像从前
2楼-- · 2019-01-08 07:44

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();
    }
查看更多
做个烂人
3楼-- · 2019-01-08 07:47

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-08 07:49

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

查看更多
我命由我不由天
5楼-- · 2019-01-08 07:53

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.

查看更多
老娘就宠你
6楼-- · 2019-01-08 07:54

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 browser in such cases.

Also remember dont 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 (Utility.isPackageExisted(context, mapsPackageName)) {
            i.setClassName(mapsPackageName, "com.google.android.maps.MapsActivity");
            i.setPackage(mapsPackageName);
        }
        context.startActivity(i);
查看更多
ら.Afraid
7楼-- · 2019-01-08 08:00
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

查看更多
登录 后发表回答