【问题标题】:Draw polyline snap to road Android google maps app绘制折线捕捉到道路Android谷歌地图应用程序
【发布时间】:2015-07-16 01:50:53
【问题描述】:

我目前正在开发一个 android 谷歌地图应用程序帮助获取地图上 2 点之间的方向。我能够从谷歌地图服务(Direction API)获得响应并根据步骤列表绘制折线(Google Maps Android SDK),但该线不贴在道路上(曲线贴在地图上的道路上),这只是一条直线。

如何在 Android 应用程序中绘制折线捕捉到道路?我正在使用 Android Studio。

这是我绘制折线的代码。

void updateMapDirection() {
    Polyline newPolyline;
    PolylineOptions options = new PolylineOptions().width(3).color(Color.BLUE).geodesic(true);
    LatLng latLng = new LatLng(mLegs.get(0).getmStartLocation().getmLatitude(),
            mLegs.get(0).getmStartLocation().getmLongitude());
    options.add(latLng);
    for (WNStep i : mLegs.get(0).getmSteps()) {
        latLng = new LatLng(i.getmEndLocation().getmLatitude(), i.getmEndLocation().getmLongitude());
        options.add(latLng);
    }
    newPolyline = map.addPolyline(options);
}

感谢您的帮助。

【问题讨论】:

标签: android google-maps polyline


【解决方案1】:

你可以这样做:

  • 一旦您将 JSON 对象作为“结果”。解析它,也以列表的形式解码折线。

    final JSONObject json = new JSONObject(result);
            JSONArray routeArray = json.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);
            JSONObject overviewPolylines = routes
                    .getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = decodePoly(encodedString);
    
  • 最后添加折线选项,方法是遍历列表并设置折线的属性,该属性对应于从 Google 地图上的 Direction API 检索到的要显示的道路。

    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < list.size(); z++) {
    LatLng point = list.get(z);
    options.add(point);
    }
    line = myMap.addPolyline(options);
    

【讨论】:

  • 请说明此答案如何解决将点捕捉到道路的问题。
猜你喜欢
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多