能够选择方向类型的谷歌地图意图



我正在使用谷歌地图意图获得方向。问题是,方向显示的是我在谷歌地图应用程序上搜索的最后一种方向。

例如,如果我最后使用谷歌地图获取步行方向,当我使用我的应用程序时,方向会自动搜索步行方向。

我想让用户能够选择他们的方向类型。

下面是一个代码片段:
String directions = "http://maps.google.com/maps?saddr=" + myLocationString + "&daddr=" + officeLocationString;
                            // Create Google Maps intent from current location to  target location
                            Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
                                    Uri.parse(directions));
                            intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                            fcontext.startActivity(intent);

我无法使用上面的"& directionmode =",它只是被忽略了。我用谷歌地图中的url在浏览器中做了一些修补,我发现"&dirflg"在浏览器和android操作系统中都工作得更好。

与以下值一起使用:r = transit, b = cyclists, d = driving, w = walking

编辑:截至2013年1月,谷歌方向API已经更新,结果现在以JSON格式提供。URL格式现在看起来像这样:

http://maps.googleapis.com/maps/api/directions/xml?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false

更多信息请点击此处。


directionsmode参数添加到您的URI中应该可以完成您正在寻找的操作。

从URL方案文档:

directionsmode:运输方式。可设置为:驾车、乘车或步行。

所以也许你可以试着这样写:

String directions = "http://maps.google.com/maps?saddr=" + myLocationString + "&daddr=" + officeLocationString + "&directionsmode=" + directionsMode;

我还没有能够使maps.google.com工作,但你可以使用替代的www.google.com/maps/dir API,如果你有origin_place_iddestination_place_id(你可以从自动完成)

path = "comgooglemapsurl://www.google.com/maps/dir/"
queryItems = [
               (name: "api", value: "1"),
               (name: "origin", value: fromPlace.text),
               (name: "origin_place_id", value: fromPlace.id),
               (name: "destination", value: toPlace.text),
               (name: "destination_place_id", value: toPlace.id),
               (name: "travelmode", value: "driving"),
    ]

您可以在这里找到模式从https替换为comgooglemapsurl的原因

最新更新