你好我想问一下当我有google map API给出的路线时如何添加Snap to Road。 我有一堆从 A 点线到 B 点线的 Lat lang 并画了一条像折线一样的线,但我想要的是如何从给定路线将此代码捕捉添加到 Road? 这是从 A 点到 B 点添加更多点的方法, 这是我想补充的, https://developers.google.com/maps/documentation/roads/snap
最佳答案
- 通过 Json 和 Gson 获取 overview_polyline(应使用 https://maps.googleapis.com/maps/api/directions/json?origin=...&destination=place_id:...&mode=DRIVING&key=.. 。)
按函数解码成List
public List<LatLng> decodePoly(String encoded) { // encoded is overview_polyline.points; List<LatLng> poly = new ArrayList<LatLng>(); 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.添加到 map :
PolylineOptions polylineOptions= new PolylineOptions(); polylineOptions.addAll(decodePoly(overview_polyline.points)); mGoogleMap.addPolyline(polylineOptions.width(5).color(Color.BLUE).geodesic(false));
关于java - 如何在 Android Studio 中添加 Snap to Roads Google Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35472005/