如何在折线上显示持续时间弹出窗口?

时间:2016-02-01 08:31:49

标签: android google-maps google-polyline

我想在Polyline上显示路线持续时间,如屏幕截图所示。我已经在互联网上做了一些挖掘,但无法找到解决方案。

screenshot

如果您有任何人有任何解决方案,请指导我。 感谢。

2 个答案:

答案 0 :(得分:0)

您可以根据位置显示标记,标记有自己的信息窗口 请参阅此链接 https://developers.google.com/maps/documentation/android-api/infowindows

答案 1 :(得分:0)

使用Google Direction API获取路线并绘制折线

在Map中添加OnPolylineClickListener并使用getTag

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

使用setTag方法

在折线中设置任何引用
                Polyline line = mMap.addPolyline(lineOptions);
                line.setTag("" + i);
                line.setClickable(true);

在折线单击中打开PopupWindow

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;
        }
    });
}