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

Aks*_*hay 46 android google-maps android-intent

我正在设计一个应用程序,我想在地图上显示特定的位置.我正在传递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);
Run Code Online (Sandbox Code Playgroud)

但它给了我谷歌地图获取方向.我知道为什么会这样,因为我用过daddr,url但我不知道具体位置使用什么..请告诉我在那里使用什么..

Rat*_*lle 118

我没有测试过这个,但你可以尝试:

第一种方法:

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

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

因此你可以将uri更改为:

第二种选择:

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

位置mTitle的名称在哪里.

第三种选择:

geo:0,0?q=my+street+address
Run Code Online (Sandbox Code Playgroud)

第四种选择:

String map = "http://maps.google.co.in/maps?q=" + yourAddress;
Run Code Online (Sandbox Code Playgroud)

希望工作和帮助:D ..

  • 那么lat和lng怎么样?我不想传递它们,我只是传递地址 (2认同)

Dar*_*hak 28

获取Lat-Lng使用此网络服务

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

然后将其传递给此代码

    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);
Run Code Online (Sandbox Code Playgroud)

我希望它会对你有所帮助

谢谢.


Ish*_*aan 21

最新版本的地图提供了更好的解决方案.如果您希望在地图上显示地址,请使用以下代码.

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);
Run Code Online (Sandbox Code Playgroud)

如果要使用纬度和经度值显示,请使用以下方法.

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);
Run Code Online (Sandbox Code Playgroud)

Lat和lng是您希望显示的纬度和经度,此处的标签是可选的.

  • +1,这是唯一未明确提及Google Maps的选项。参数“?q = lat,lon”也有更好的结果,因为它在打开的地图上显示了图钉(第一个lat,lon仅将地图平移到该位置)。setPackage调用是可选的。 (2认同)

Har*_*edi 10

你应该使用这样的东西

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);
Run Code Online (Sandbox Code Playgroud)

并放一针

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();
    }
Run Code Online (Sandbox Code Playgroud)


siv*_*ag1 8

这将像魅力一样发挥作用。确保检查谷歌地图是否存在,这样它也可以在所有非谷歌设备上通用。在这种情况下,它将在浏览器中打开。

另外,请记住,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;
}
Run Code Online (Sandbox Code Playgroud)


xom*_*ena 5

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

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

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

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

代码片段是

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);
Run Code Online (Sandbox Code Playgroud)