Google地图:绘图路线的起点和终点已连接

时间:2018-10-30 18:24:15

标签: android google-polyline maproute

我正在使用航点在Google地图上绘制路线。一切看起来不错,除了我的出发地和目的地也已连接。我确定我的出发地和目的地不一样。

这是我从纬度/经度列表中生成路线网址后生成的网址。

https://maps.googleapis.com/maps/api/directions/json? &origin = 26.8530478,75.78491989999999 &destination = 26.8804683,75.75850109999999 &waypoints = 26.8566917,75.7691974%7C 26.868405,75.76440269999999%7C 26.8762989,75.7664082 &key = MY_API_KEY

并附加输出。从“ 1”到“ 5”的直线不应该存在。请帮助我找出错误。

谢谢。

ParserTask-

ArrayList<LatLng> points = new ArrayList<LatLng>();
    PolylineOptions lineOptions = new PolylineOptions();
   // LatLngBounds.Builder builder = new LatLngBounds.Builder();
    int color = ContextCompat.getColor(activity, R.color.black_overlay);
    if (null != result)
        for (int i = 0; i < result.size(); i++) {
            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);
            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);
                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);
                points.add(position);
                //builder.include(position);
            }
            lineOptions = new PolylineOptions().width(15).color(color).addAll(points).zIndex(6);
        }
    if (activity instanceof OnRoutesParsingCompletedCallBack) {
        ((OnRoutesParsingCompletedCallBack) activity).
                onRoutesParsingCompleted(lineOptions);
    }

具有地图对象的活动-

@Override
public void onRoutesParsingCompleted(PolylineOptions lineOptions) {//}, LatLngBounds.Builder builder) {
    if (lineOptions.getPoints().size() > 0) {
        googleMap.addPolyline(lineOptions);
    } else
        Toast.makeText(this, "There is something wrong. No routes to draw!", Toast.LENGTH_SHORT).show();

    mCurrLocationMarker.showInfoWindow();
    progressBar.setVisibility(GONE);
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLong, 13));
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为,问题在于您正在将所有可能的路线添加到同一条折线中。因此,折线沿第一条路线走,然后“返回”到起点,然后再次沿第二条路线走。您或者需要删除for循环遍历结果:

if (null != result)
    // Fetching 1st route
    List<HashMap<String, String>> path = result.get(0);
    // Fetching all the points in 1st route
    for (int j = 0; j < path.size(); j++) {
        HashMap<String, String> point = path.get(j);
        double lat = Double.parseDouble(point.get("lat"));
        double lng = Double.parseDouble(point.get("lng"));
        LatLng position = new LatLng(lat, lng);
        points.add(position);
        //builder.include(position);
    }
    lineOptions = new PolylineOptions().width(15).color(color).addAll(points).zIndex(6);
}

或者您可以为每个现有路线创建一条折线。