3

I want to show route duration on Polyline as shown on the screenshot. I have done some digging on internet but couldn't find the solution.

screenshot

Guide me if anyone of you have any solution. Thanks.

activesince93
  • 1,716
  • 17
  • 37

2 Answers2

0

you can show markers according to locations and markers have their own info windows refer to this link https://developers.google.com/maps/documentation/android-api/infowindows

  • Info windows are always anchored to a marker. I want to anchor it to `Polyline`. – activesince93 Feb 01 '16 at 10:54
  • You can use [Symbols](https://developers.google.com/maps/documentation/javascript/symbols#properties) if you want to add image in your polyline. A symbol is a vector-based image defined by a path, using SVG path notation. To [add a symbol to a polyline](https://developers.google.com/maps/documentation/javascript/symbols#add_to_polyline), set the `icons[]` property of the `PolylineOptions` object. This [link](http://stackoverflow.com/questions/8247145/how-to-put-a-infowindow-on-polyline-in-google-maps-v3) might also help. – abielita Feb 02 '16 at 04:06
0

Use Google Direction API to get the route and draw the polyline

Add OnPolylineClickListener in Map and get the reference using getTag

    mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
        @Override
        public void onPolylineClick(Polyline polyline) {
            Log.e("Polyline position", " -- " + polyline.getTag());
            onButtonShowPopupWindowClick("  " + polyline.getTag());
        }
    });

Set any reference in polyline using setTag method

                Polyline line = mMap.addPolyline(lineOptions);
                line.setTag("" + i);
                line.setClickable(true);

Open PopupWindow in polyline click

public void onButtonShowPopupWindowClick(String count) {

    String[] timeDistance = count.split(",");

    // get a reference to the already created main layout
    LinearLayout mainLayout = (LinearLayout)
            findViewById(R.id.whole_layout);

    // inflate the layout of the popup window
    LayoutInflater inflater = (LayoutInflater)
            getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.polyline_window, null);

    ((TextView) popupView.findViewById(R.id.time)).setText("30 mins");
    ((TextView) popupView.findViewById(R.id.distance)).setText("20 km");

    // create the popup window
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;
    boolean focusable = true; // lets taps outside the popup also dismiss it
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

    // show the popup window
    popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

    // dismiss the popup window when touched
    popupView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            popupWindow.dismiss();
            return true;
        }
    });
}
Thamim
  • 328
  • 3
  • 14