62

我正在设计一个应用程序,我想在其中显示地图上的特定位置。我正在传递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);

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

4

13 回答 13

139

我没有对此进行测试,但您可以尝试:

第一种方法:

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

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

因此您可以将 uri 更改为:

第二种选择:

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

wheremTitle是位置的名称。

第三个选项:

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

第四种选择:

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

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

于 2014-03-28T05:02:57.073 回答
36

使用此网络服务获取 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);

我希望它会帮助你

谢谢你。

于 2014-03-28T05:05:08.080 回答
30

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

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

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

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 和 lng 是您希望显示的纬度和经度,此处的标签是可选的。

于 2016-09-12T06:46:26.413 回答
12

你应该使用这样的东西

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

并放下一个别针

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();
    }
于 2014-03-28T04:56:21.943 回答
8

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

另外,请记住,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;
}
于 2015-01-03T05:04:48.433 回答
3

从 2020 年起,您还可以考虑使用Google Maps URLs,这是由 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);

我希望这有帮助!

于 2020-01-25T22:19:08.153 回答
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);
}

请参阅此文档

于 2016-09-21T14:21:38.823 回答
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();
                }
            }
        });
于 2017-09-27T12:30:23.740 回答
2

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + address)); 开始活动(意图);

于 2021-02-26T17:15:09.623 回答
1

如果设备上没有可以打开地图的活动,此帮助方法使用 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;
}
于 2017-06-14T13:35:42.547 回答
0

对于那些正在寻找打开特定位置的“地图”应用程序(通过传递纬度和经度)并且不希望在重定向后在地图搜索栏中显示纬度/经度坐标的人(打开地图应用程序) . 而不是希望显示一些标签地名,您可以参考下面的代码,

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

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

于 2020-01-24T07:14:39.403 回答
0

要在谷歌地图中显示位置并显示显示方向按钮,请使用以下代码片段:

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);
于 2020-03-04T12:58:01.143 回答
-1

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

字符串位置 = " https://www.google.com/maps/dir// " + 纬度 + "," + 经度;尝试 { SmsManager 短信 = SmsManager.getDefault(); // 使用 android SmsManager sms.sendTextMessage(number, null, message + location, null, null); // 添加数字和文本 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(); }

于 2019-08-13T09:51:41.653 回答