简体   繁体   中英

How to join multiple markers on the map with arrow headed poly-line to show directions of travelling?

I am a newbie to the android and currently working on the Google-map API. I am able to plot multiple markers on the map but want to join multiple markers with poly line.I have referred this for the directions concern but it is for two points only. Below is the code for the Activity:

 public class MainActivity extends AppCompatActivity {

// Google Map
private GoogleMap googleMap;
// latitude and longitude
double latitude;
double longitude;
String newtime;
ArrayList<LatLng> points;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    points = new ArrayList<LatLng>();
    points.add(new LatLng(21.114369, 79.049423));
    points.add(new LatLng(21.113913, 79.049203));
    points.add(new LatLng(21.113478, 79.048736));
    points.add(new LatLng(21.113002, 79.048592));
    points.add(new LatLng(21.112857, 79.047315));
    points.add(new LatLng(21.112997, 79.046741));
    try {
        // Loading map
        initilizeMap();

    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap.getUiSettings().setZoomControlsEnabled(true);
    googleMap.setMyLocationEnabled(true);

    SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd-MM-yy HH:mm:ss", Locale.US);
    newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));

    // googleMap.addMarker(marker);
    drawMarker(points);

}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}

private void drawMarker(ArrayList<LatLng> l) {
    // Creating an instance of MarkerOptions

    for (int i = 0; i < l.size(); i++) {
        latitude = l.get(i).latitude;
        longitude = l.get(i).longitude;
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Bus")
                .snippet(newtime);
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // Adding marker on the Google Map
        googleMap.addMarker(marker);
    }

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(l.get(0).latitude, l.get(0).longitude)).zoom(18).build();

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
}

}

Please help/guide me to achieve the task.

~Thanks.

Modify your drawMarker() to the following:-

private void drawMarker(ArrayList<LatLng> l) {
    // Creating an instance of MarkerOptions

    PolylineOptions options = new PolylineOptions();
    options.color(Color.RED);

    for (int i = 0; i < l.size(); i++) {
        options.add(l.get(i));
        MarkerOptions marker = new MarkerOptions().position(l.get(i)).title("Bus")
                .snippet(newtime);
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // Adding marker on the Google Map
        googleMap.addMarker(marker);
    }

    googleMap.addPolyline(options);

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(l.get(0).latitude, l.get(0).longitude)).zoom(18).build();

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

Here it will add lines with red color, to change colors as your wish just modify the options.color() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM