我的应用程序需要显示谷歌地图方向从A到B,但我不想把谷歌地图到我的应用程序-相反,我想用一个意图启动它。这可能吗?如果是,怎么做?


当前回答

这有点跑题了,因为你问了“方向”,但你也可以使用Android文档中描述的Geo URI方案:

http://developer.android.com/guide/appendix/g-app-intents.html

使用“geo:latitude,longitude”的问题是谷歌Maps仅以您的点为中心,没有任何大头针或标签。

这很让人困惑,尤其是当你需要指明一个确切的地方或问路的时候。

如果使用查询参数“geo:lat,lon?”Q =name”为了标记您的地理点,它使用查询来搜索并取消lat/lon参数。

我找到了一种方法,以纬度/lon居中的地图和显示一个自定义标签的大头针,非常好的显示和有用的时候,询问方向或任何其他行动:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")"));
startActivity(intent);

注意(by @TheNail):在地图v.7中不能工作(撰写本文时的最新版本)。将忽略坐标并搜索圆括号之间具有给定名称的对象。请参见谷歌将7.0.0映射为location的Intent

其他回答

你可以通过这种方式在Android上启动谷歌地图方向

btn_search_route.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String source = et_source.getText().toString();
            String destination = et_destination.getText().toString();

            if (TextUtils.isEmpty(source)) {
                et_source.setError("Enter Soruce point");
            } else if (TextUtils.isEmpty(destination)) {
                et_destination.setError("Enter Destination Point");
            } else {
                String sendstring="http://maps.google.com/maps?saddr=" +
                        source +
                        "&daddr=" +
                        destination;
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                        Uri.parse(sendstring));
                startActivity(intent);
            }
        }

    });

首先你需要知道,现在你可以使用隐式意图,android文档为我们提供了一个非常详细的通用意图 为了实现映射意图,你需要创建一个带有两个参数的新意图

行动 尤里

对于动作,我们可以使用Intent。ACTION_VIEW 对于Uri,我们应该构建它,下面我附上了一个示例代码来创建,构建,启动活动。

 String addressString = "1600 Amphitheatre Parkway, CA";

    /*
    Build the uri 
     */
    Uri.Builder builder = new Uri.Builder();
    builder.scheme("geo")
            .path("0,0")
            .query(addressString);
    Uri addressUri = builder.build();
    /*
    Intent to open the map
     */
    Intent intent = new Intent(Intent.ACTION_VIEW, addressUri);

    /*
    verify if the devise can launch the map intent
     */
    if (intent.resolveActivity(getPackageManager()) != null) {
       /*
       launch the intent
        */
        startActivity(intent);
    }

这对我来说很管用:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://maps.google.co.in/maps?q=" + yourAddress));
if (intent.resolveActivity(getPackageManager()) != null) {
   startActivity(intent);
}

谷歌方向视图,源位置作为当前位置,目标位置作为给定的字符串

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&daddr="+destinationCityName));
intent.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
}

在上面的destinationCityName是一个字符串变量,可以根据需要进行修改。

对于多个路径点,也可以使用以下方法。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("https://www.google.com/maps/dir/48.8276261,2.3350114/48.8476794,2.340595/48.8550395,2.300022/48.8417122,2.3028844"));
startActivity(intent);

第一组坐标是你的起始位置。接下来的所有点都是路径点,都是经过的路线。

只要在末尾连接“/latitude,longitude”来继续添加路径点。根据谷歌文档,显然有23个路径点的限制。不确定这是否也适用于Android。