如何在Android Studio中添加Snap to Roads Google Map

您好我想问一下当我有google map API给出的路线时如何添加Snap to Road。 我从A点到B点有一堆Lat lang并绘制一条像Polylines这样的线,但我想要的是如何从给定的路径添加此代码snap到Road? 这是如何从点A到点B添加更多点,这是我要添加的内容, https://developers.google.com/maps/documentation/roads/snap

我的项目看起来像这样 在此处输入图像描述

  1. 通过Json和Gson获取overview_polyline(应使用https://maps.googleapis.com/maps/api/directions/json?origin=…&destination=place_id:…&mode=DRIVING&key= .. )
  2. 按function将其解码为List

    public List decodePoly(String encoded) { // encoded is overview_polyline.points; List poly = new ArrayList(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5))); poly.add(p); } return poly; } 

    3.添加到地图:

     PolylineOptions polylineOptions= new PolylineOptions(); polylineOptions.addAll(decodePoly(overview_polyline.points)); mGoogleMap.addPolyline(polylineOptions.width(5).color(Color.BLUE).geodesic(false));