【问题标题】:Is there a way to show road directions in Google Map API v2? [duplicate]有没有办法在 Google Map API v2 中显示道路方向? [复制]
【发布时间】:2013-01-28 00:56:23
【问题描述】:

我正在使用谷歌和这里寻找答案,我发现的唯一相关帖子是:

Google Maps Android V2 and Direction API

Get driving directions using Google Maps API v2

但那里没有答案。所以我已经提到过,但我会再说一遍。我正在寻找使用 FragmentActivity 和 SupportMagFragment 和 LatLng 对象而不使用 MapView 、MapActivity 和 GeoPoint 的 Google Map API v2 解决方案。

此外,我没有要使用的 Overlay 对象,因此我无法在地图上绘制方向,是否有替代方法?

那么有没有办法做到这一点?

【问题讨论】:

  • 看来这个问题完全被另一个人回答了。我建议将此问题标记为重复问题,以向未来的读者指明正确答案。

标签: android google-maps driving-directions google-directions-api


【解决方案1】:

试试这个解决方案here。您可以在 V2 上获取驾车或步行方向。

【讨论】:

  • 伟大的@Akexorcist,您应该添加到此解决方案的唯一内容是“getDocument”方法应该在异步任务中运行。这个解决方案已经过我的测试,在做出改变后它就可以工作了。谢谢。
【解决方案2】:

叠加层确实值得忘记。

折线很容易画出来

https://developers.google.com/maps/documentation/android/lines#add_a_polyline

解析 JSON 响应后,只需遍历您的点:

PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Set the rectangle's color to red
rectOptions.color(Color.RED);

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

【讨论】:

  • 我会试试的,谢谢。
【解决方案3】:

您的问题标题比您的要求更笼统,因此我会以一种我认为对查看此问题的人有益的方式回答这个问题,并希望以不同的方式满足您的要求。

如果您没有在已经加载的片段的地图上下文中显示方向,并且已经做了一些事情来显示地图上的方向(这可能类似于 OP 正在做的事情),它会更容易并且我相信标准使用Intent 执行此操作。

这将启动一个地图路径活动(通过一个单独的应用程序 - 启动的应用程序取决于用户的兼容应用程序,默认情况下是 Google 地图),它绘制从源地址 (String originAddress) 到 经由道路的目的地地址 (String destinationAddress):

// Build the URI query string.
String uriPath = "https://www.google.com/maps/dir/";
// Format parameters according to documentation at:
// https://developers.google.com/maps/documentation/directions/intro
String uriParams = 
    "?api=1" +
    "&origin=" + originAddress.replace(" ", "+")
        .replace(",", "") +
    "&destination=" + destinationAddress.replace(" ", "+")
        .replace(",", "") +
    "&travelmode=driving";
Uri queryURI = Uri.parse(uriPath + uriParams);
// Open the map.
Intent intent = new Intent(Intent.ACTION_VIEW, queryURI);
startActivity(activity, intent, null);

(其中activity 只是当前活动的Activity - 通过当前编程上下文中适当的任何方式获得)。

以下代码从LatLng 对象获取地址String(然后必须为URI 查询String 进行处理,如上所述):

/**
* Retrieves an address `String` from a `LatLng` object.
*/
private void getAddressFromLocation(
    final StringBuilder address, final LatLng latlng) {
    // Create the URI query String.
    String uriPath = 
        "https://maps.googleapis.com/maps/api/geocode/json";
    String uriParams = 
        "?latlng=" + String.format("%f,%f",
            latlng.latitude, latlng.longitude) +
        "&key=" + GOOGLE_MAPS_WEB_API_KEY;
    String uriString = uriPath + uriParams;
    // Issue the query using the Volley library for networking.
    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    JSONObject response = null;
    // Required for JsonObjectRequest, but not important here.
    Map<String, String> jsonParams = new HashMap<String, String>();
    JsonObjectRequest request = 
        new JsonObjectRequest(Request.Method.POST, 
            uriString,
            new JSONObject(jsonParams),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        if (response != null) {
                            String resultString =
                                response.getJSONArray("results")
                                        .getJSONObject(0)
                                        .getString("formatted_address");
                            // Assumes `address` was empty.
                            address.append(resultString);
                        } // end of if
                        // No response was received.
                    } catch (JSONException e) {
                        // Most likely, an assumption about the JSON 
                        // structure was invalid.
                        e.printStackTrace();
                    }
                } // end of `onResponse()`
            }, // end of `new Response.Listener<JSONObject>()`
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(LOG_TAG, "Error occurred ", error);
                }
            });
    // Add the request to the request queue.
    // `VolleyRequestQueue` is a singleton containing
    // an instance of a Volley `RequestQueue`.
    VolleyRequestQueue.getInstance(activity)
        .addToRequestQueue(request);
}

这个请求是异步的,但是it can be made synchronous。 您需要在传递给address 的实际参数上调用toString() 以获取originAddress

【讨论】:

    【解决方案4】:

    问:我一直在使用 Google 和此处寻找答案,并且是唯一的 我发现的相关帖子是:Google Maps Android V2 and Direction API Google map API v2 - 获取行车路线

    答案:你说,“但那里没有答案。”不是绝对正确的。在此站点中,您只能找到一些关于此的线索。我认为你不会在这里得到完美的代码和具体的实现 KNOW-HOWs。事实上,很多开发者都想让应用程序在谷歌地图上显示路线或方向。但我认为仅使用纯 Google Maps API v2 没有解决方案来获取路线。

    问:所以我已经提到过,但我会再说一遍。我是 使用 Google Map API v2 寻找解决方案 FragmentActivity 和一个 SupportMagFragment 和 LatLng 对象而不是 使用 MapView 、MapActivtiy 和 GeoPoint。

    答案:这里是a few good sample tutorials (click here)。你可以找到你想要的。

    问:另外我没有可以使用的 Overlay 对象,所以我无法绘画 地图上的方向,是否有替代方案?也是 有办法吗?

    答案:在 Google Maps API v2 中,不再需要烦人的 Overlay 等。为此,您可以在我上面的链接网站中找到答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多