如何让 touchlistener 在 mapview 上有更长的触摸时间

问题描述 投票:0回答:0

所以我在 Android Studio 中使用 OpenStreetMap 创建一个路线规划器应用程序,我想让用户能够点击地图并在该位置添加标记,这样添加标记的过程就像这样

  1. 用户点击他们想要在地图上的任何地方。
  2. 然后用户会看到一个对话框,询问他们“在此处添加标记?”是或否
  3. 用户点击是,他们被带到我创建的表格。
  4. 在这种形式下,用户选择标记的类别,例如 Speed van。此表单中还有两个文本框,它们会自动填充标记位置的经度和纬度。
  5. 然后用户点击提交按钮,将他们选择的类别和经纬度上传到 Firebase 数据库,然后标记显示在地图上,用户被带回具有地图视图的默认页面。

到目前为止,我还没有完成 firebase 的工作,但是每当我单击地图甚至移动地图以查看它时,都会出现此对话框,我想将其更改为如果用户单击地图视图超过2秒或类似的东西然后对话框出现

这是我的标记的 Java 代码:


        //Creating a new marker that is to be used by the user
        marker = new Marker(mapView);
        marker.setIcon(getResources().getDrawable(R.drawable.ic_marker));
        marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);

        mapView.getOverlays().add(marker);

        mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    GeoPoint tapLocation = (GeoPoint) mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
                    marker.setPosition(tapLocation);
                    alertDialog.show();
                }
                return false;
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Add a marker here?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Launch your form activity and pass the latitude and longitude of the clicked position
                Intent intent = new Intent(MainActivity.this, MarkerOnMapActivity.class);
                intent.putExtra("latitude", marker.getPosition().getLatitude());
                intent.putExtra("longitude", marker.getPosition().getLongitude());
                startActivity(intent);

            }

        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                mapView.getOverlays().remove(marker);
            }
        });
        alertDialog = builder.create();

我厌倦了改用 onLongClickListner 方法,但这对我来说根本不起作用,但这是我不使用的 onLongClickListener 的代码:

 // Add long-click listener to the map
        mapView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // Show confirmation dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("Add a marker here?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Start new activity to display form
                                Intent intent = new Intent(MainActivity.this, MarkerOnMapActivity.class);
                                intent.putExtra("latitude", mapView.getMapCenter().getLatitude());
                                intent.putExtra("longitude", mapView.getMapCenter().getLongitude());
                                startActivity(intent);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });
                builder.create().show();
                return true;
            }
        });

也很累 setOnMapLongClickListener 对我也不起作用

java openstreetmap onclicklistener osmdroid
© www.soinside.com 2019 - 2024. All rights reserved.