There are points (coordinates) need to draw a route. Are there any?
Closed due to the fact that off-topic participants aleksandr barakin , Alexey Shimansky , cheops , Lex Hobbit , tutankhamun Aug 21 '17 at 10:40 .
- Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
- fiveThis question should be closed, because questions about where to find programs, libraries, textbooks on programming and administration are not worth asking - aleksandr barakin
|
2 answers
To do this, do not need any. This is done using standard Google tools.
First you need to get a route. Use the Directions API for this.
In response, you will receive all the information on the route, including navigation. If you just need to draw a route, then we will be interested in the answer "routes" for getting route points and "bounds", to determine the region for scaling the map to show the route completely.
Example of parsing the answer and displaying the route on the map:
private GoogleMap map; ... final JSONObject json = new JSONObject(здесь ответ от Directions API); if(json.getString("status").equals("OK")) { JSONArray jsonRoutes = json.getJSONArray("routes"); JSONObject jsonRoute = jsonRoutes.getJSONObject(0); final String points = jsonRoute.getJSONObject("overview_polyline").getString("points"); final JSONObject jsonBounds = jsonRoute.getJSONObject("bounds"); final JSONObject jsonNortheast = jsonBounds.getJSONObject("northeast"); final JSONObject jsonSouthwest = jsonBounds.getJSONObject("southwest"); LatLngBounds.Builder builder = new LatLngBounds.Builder(); builder.include(new LatLng(jsonNortheast.getDouble("lat"), jsonNortheast.getDouble("lng"))); builder.include(new LatLng(jsonSouthwest.getDouble("lat"), jsonSouthwest.getDouble("lng"))); final LatLngBounds latLngBounds = builder.build(); PolylineOptions polylineOptions = new PolylineOptions(); polylineOptions.color(Color.BLACK); polylineOptions.width(getPixelsForDp(6 /* ширина линии в dp */, getResources())); polylineOptions.addAll(decodePolyLine(points)); // теперь можно рисовать маршрут на карте map.addPolyline(polylineOptions); CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(latLngBounds, getPixelsForDp(64 /* размер отступа от краев региона в dp (padding) */, getResources())); map.animateCamera(cameraUpdate); } else { // Гугл вернул ошибку new Throwable("Route Exception!"); } ... // переводит закодированную строку PolyLine в список координат private List<LatLng> decodePolyLine(final String poly) { int len = poly.length(); int index = 0; List<LatLng> decoded = new ArrayList<>(); int lat = 0; int lng = 0; while (index < len) { int b; int shift = 0; int result = 0; do { b = poly.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 = poly.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; decoded.add(new LatLng(lat / 100000d, lng / 100000d)); } return decoded; } ... // возвращает пиксельный размер для заданного dp private int getPixelsForDp(int sizeInDp, Resources resources) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDp, resources.getDisplayMetrics()); } - If I give him a list of points, will he give me a route tied to the roads? - Eugene Zaychenko
- @EugeneZaychenko Yes, or the coordinates of the starting and ending points or their address, read the documentation, I gave the link. It will return not only the route, but also other data, including navigation (how to get there). - mit
|
There is a manual in Google, if I understood correctly, https://developers.google.com/maps/documentation/android-api/utility/
- I do not need a manual, the standard API has already tried everything, it is not convenient. Question about convenient libraries. - Eugene Zaychenko
- one
- This is more like, I thought maybe there are some popular ones I don’t know about ?! - Eugene Zaychenko
|