13

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

Google Maps Android V2 和 Direction API

使用 Google Maps API v2 获取行车路线

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

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

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

4

4 回答 4

9

在此处尝试此解决方案。您可以在 V2 上获取驾驶或步行方向。

于 2013-02-24T16:40:54.220 回答
4

叠加层确实值得忘记。

可以轻松绘制折线

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);
于 2013-01-28T01:09:07.337 回答
1

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

如果您没有在已经加载的片段的地图上下文中显示方向,并且已经做了一些事情来显示地图上的方向(这可能类似于 OP 正在做的事情),那么它会更容易并且我相信这样做是标准的带有Intent.

这将启动一个地图路径活动(通过一个单独的应用程序 - 启动的应用程序取决于用户的兼容应用程序,默认情况下是谷歌地图),该活动绘制从起始地址 ( 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- 通过当前编程上下文中适当的任何方式获得)。

下面的代码String从一个LatLng对象中获取一个地址(然后必须String为上面的 URI 查询进行处理):

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

此请求是异步的,但可以设为同步。您将需要调用toString()传递给的实际参数address来获取originAddress.

于 2017-07-20T17:55:29.537 回答
-1

问:我正在使用谷歌和这里寻找答案,我发现的唯一相关帖子是:谷歌地图 Android V2 和方向 API 谷歌地图 API v2 - 获取行车路线

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

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

答:这里有一些很好的示例教程(点击这里)。你可以找到你想要的。

问:另外我没有使用 Overlay 对象,所以我无法在地图上绘制方向,是否有替代方法?那么有没有办法做到这一点?

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

于 2013-01-28T03:22:56.847 回答