通过意图打开谷歌地图在android中的特定位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22704451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:21:20  来源:igfitidea点击:

open google maps through intent for specific location in android

androidgoogle-mapsandroid-intentgoogle-maps-urls

提问by Akshay

I'm designing one application in which I want to show specific location on Map. I'm passing Stringof address which is already placed on Google Map. Following is my Intentcode..

我正在设计一个应用程序,我想在其中显示地图上的特定位置。我正在传递String已经放在Google Map. 以下是我的Intent代码..

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 daddrin urlbut I don't know what to use for specific location..Please tell me what to use there..

但它给了我谷歌地图以获得方向。我知道为什么会这样,因为我使用daddrurl但我不知道在特定位置使用什么..请告诉我在那里使用什么..

回答by Rat-a-tat-a-tat Ratatouille

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

编辑:这可能不适用于 Google 地图 7,0

hence you could change the uri to :

因此,您可以将 uri 更改为:

Second option:

第二种选择:

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

where mTitleis the name of the location.

位置mTitle的名称在哪里。

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

希望有效并有所帮助:D ..

回答by Darshak

Get Lat-Lng Using this web-service

使用此网络服务获取纬度

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

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.

谢谢你。

回答by Ishaan

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.

Lat 和 lng 是您希望显示的纬度和经度,这里的标签是可选的。

回答by Hardik Trivedi

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

回答by sivag1

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.

另外,请记住,URL 中不要包含 www。否则这将不起作用。

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

回答by jobinrjohnson

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

请参阅此文档

回答by Rehan Sarwar

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

回答by HendraWD

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

如果设备上没有可以打开地图的活动,则此辅助方法使用 uriBuilder 来获得更清晰的代码和处理条件

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

回答by Avi

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 barafter redirection(Opening Maps app). Instead wish to show some labelor 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.

注意:上述方法中的“locationName”表示您希望在地图搜索栏中显示的名称。

回答by xomena

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:

从 2020 年开始,您还可以考虑使用Google Maps URLs,这是 Google 设计的用于创建通用跨平台链接的 API。您可以使用以下形式的相同 URL 字符串在网络、Android 或 iOS 上打开 Google 地图:

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

https://www.google.com/maps/search/?api=1¶meters

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

如果您需要显示某个位置,您可以使用类似的 URL

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

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!

我希望这有帮助!