Google Map API v2中有显示道路方向的方法吗?

13

我在Google和这里都找到了答案,但是唯一相关的帖子是:

Google地图Android V2和Direction API

使用Google Maps API v2获取驾驶路线

但那里没有答案。所以我已经提到过了,但我还是要再说一遍。我正在寻找一种解决方案,可以使用FragmentActivity和SupportMagFragment以及LatLng对象来处理Google Map API v2,而不是使用MapView、MapActivity和GeoPoint。

此外,我没有Overlay对象可用于绘制地图上的方向,是否有替代方法?

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


看起来这个问题已经被另一个问题完全回答了。我建议将此问题标记为重复,以指示未来读者正确的答案。 - halfer
4个回答

9

在这里尝试这个解决方案您可以在V2上获取驾车或步行路线。


4
伟大的 @Akexorcist,你需要在这个解决方案中添加的唯一一件事情就是“getDocument”方法应该在Async-Task中运行。经过我的测试,在进行这个更改之后,这个解决方案可以工作。谢谢。 - Emil Adz

4

Overlay确实是易被遗忘的内容。

可以轻松地绘制折线。

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

1

您的问题标题比您的要求更加通用,因此我会以一种我认为有益于查看此问题的人并希望以不同的方式满足您的要求来回答。

如果您还没有在地图上显示方向,并且已经完成了显示地图上的方向的操作(这可能类似于OP正在进行的操作),则使用Intent更容易且更标准。

这将启动一个地图路径规划活动(通过单独的应用程序 - 启动的应用程序取决于用户的兼容应用程序,默认情况下是Google Maps),该活动通过道路绘制从起点地址(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);
}

这个请求是异步的,但它可以变成同步的。你需要在传递给address的实际参数上调用toString()以获得originAddress

-1
问:我在谷歌和这里寻找答案,但是唯一相关的帖子是:Google Maps Android V2 和 Direction API Google map API v2 - get driving directions。
回答:你说“但那里没有答案”并不完全正确。在这个网站上,你只能找到一些线索。我认为你不会在这里找到完美的代码和具体的实现方法。事实上,许多开发者想要制作应用程序,在Google Maps上显示路线或方向。但我认为,仅使用纯Google Maps API v2无法获得方向。
问:所以我已经提到了,但我还是要再说一遍。我正在寻找一个解决方案,使用FragmentActivity和SupportMagFragment和LatLng对象,而不是使用MapView、MapActivtiy和GeoPoint的Google Map API v2。
回答:这里有几个好的样本教程(点击此处)。你可以找到你想要的东西。

问:另外,我没有Overlay对象可用,因此无法在地图上绘制方向,有替代方法吗?是否有办法做到这一点?

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接