How to draw route between two points in MapFragment using Google Maps Android API

十年热恋 提交于 2019-12-03 09:48:58

You will need direction api. Here is good tutorial:

https://www.androidtutorialpoint.com/intermediate/google-maps-draw-path-two-points-using-google-directions-google-map-android-api-v2/

You can put all code in one class. But it is better to use multiple class.

If you are already using the Google Maps Fragment and just want to add lines to the map (i.e. record a route vs plot a route), adding a line (called a poly line) is trivial.

Start by creating a PolyLineOptions (saving the reference allows you to update it later):

PolylineOptions routeLine;
LatLng pos = new LatLng(location.getLatitude(), location.getLongitude());
routeLine = new PolylineOptions().add(pos);

At this point the line won't show, to make it show there are two requirements: Add it to the map and Add at least one more point:

routeLine.add(new LatLng(nextLocation.getLatitude(), nextLocation.getLongitude()));
map.addPolyline(routeLine);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!