-->

有没有一种方法来显示谷歌地图API第2路的方向? [重复](Is there a way to

2019-07-18 19:38发布

这个问题已经在这里有一个答案:

  • 获得使用谷歌地图API V2行车路线 3个回答

我一直在寻找使用谷歌和这里的答案,我发现的唯一有关的职位是:

谷歌地图Android版V2和方向API

获得使用谷歌地图API V2行车路线

但没有答案在那里。 所以,我已经提到过,但我会再说一遍。 我要寻找使用FragmentActivity和SupportMagFragment和经纬度对象谷歌地图API第2版的解决方案,而不是使用MapView类,MapActivity和的GeoPoint。

此外,我没有覆盖对象使用,所以我不能在地图上画的方向,是否有针对的方法吗?

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

Answer 1:

尝试这种解决方案在这里 。 你可以开车或者走路V2方向。



Answer 2:

叠加确实让人忘记。

折线可以很容易地得出

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

刚刚经历yourr点循环您解析tjhe 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);


Answer 3:

你的问题的标题是不是你的要求更普遍,所以我会回答这个问题的方式,我认为将有利于那些观看了这个问题,并希望满足可能以不同的方式您的要求。

如果你不显示的地图已经被加载的片段和一些上下文的方向已经完成,以显示在地图上的方向(这可能是类似于什么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对象(它必须然后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);
}

该请求是异步的,但是它可以制成同步 。 您将需要调用toString()传递给实际参数address获得originAddress



Answer 4:

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

答:你的说法,“但没有答案在那里。” 不完全正确。 在这个网站上,你可以找到,只是只有少数线索。 我想你不会得到在这里完美的代码和具体实施KNOW-怎么样了。 事实上,很多开发商想使应用程序显示在谷歌地图的路线或方向。 但我认为没有解决方案,使仅仅只用纯正的谷歌地图API V2的方向。

问:我已经提到过,但我会再说一遍。 我要寻找使用FragmentActivity和SupportMagFragment和经纬度对象谷歌地图API第2版的解决方案,而不是使用的MapView,MapActivtiy和的GeoPoint。

答:这里有几个好样的教程(点击这里) 。 你可以找到你想要的东西。

问:除了我没有覆盖对象使用,所以我不能在地图上画的方向,是否有针对的方法吗? 那么,有没有办法做到这一点?

答:在谷歌地图API V2,恼人的覆盖等方面都没有什么更多的要求。 对于这一点,你可以找到在我上面的链接网站的答案。



文章来源: Is there a way to show road directions in Google Map API v2? [duplicate]