在地图上创建并显示标记单击

时间:2019-12-09 11:44:42

标签: java android google-maps google-maps-markers

我正在尝试在我的Google地图上显示两个标记。一种是用户双击地图,另一种是用户双击地图。下面是我的代码

  mMap=googleMap;
LatLng Kuta=new LatLng(-12.8232,28.2176);
mMap.setLatLngBoundsForCameraTarget(Kitwe);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setZoomGesturesEnabled(false);
marker2=mMap.addMarker(new MarkerOptions().position(Kuta)
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))

        .title("kk").visible(false));
marker1=mMap.addMarker(new MarkerOptions().position(Kuta)
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))

        .title("kk").visible(false));
 mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {

        i++;
        Handler handler=new Handler();
      Runnable r=new Runnable() {
          @Override
           public void run() {
               i=0;
            }
        };
        if (i==1){
           Toast.makeText(getApplicationContext(),"too late",Toast.LENGTH_SHORT).show();
            handler.postDelayed(r,250);
      }else if (i==2){
           Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();
           marker1.remove();
           marker1.setPosition(latLng);
           marker1.setVisible(true);
          // marker1(latLng)

handler.postDelayed(r,250);
}else if (i==3){
           Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_SHORT).show();
            marker1.remove();
            marker1.setPosition(latLng);
            marker1.setVisible(true);


           i=0;
      }
    }
});

当我双击但什么都没有发生时,当我三次单击时也什么也没有发生,请提供帮助。我希望标记分别出现在双击和三次单击的位置。

1 个答案:

答案 0 :(得分:0)

首先避免使用三次轻击,因为用户不习惯这种应用行为。

如果您仍然希望使用它,可以执行以下操作:

view.setOnTouchListener(new View.OnTouchListener() {
    Handler handler = new Handler();

    int numberOfTaps = 0;
    long lastTapTimeMs = 0;
    long touchDownMs = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchDownMs = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacksAndMessages(null);

                if ((System.currentTimeMillis() - touchDownMs) > ViewConfiguration.getTapTimeout()) {
                    //it was not a tap

                    numberOfTaps = 0;
                    lastTapTimeMs = 0;
                    break;
                }

                if (numberOfTaps > 0 
                        && (System.currentTimeMillis() - lastTapTimeMs) < ViewConfiguration.getDoubleTapTimeout()) {
                    numberOfTaps += 1;
                } else {
                    numberOfTaps = 1;
                }

                lastTapTimeMs = System.currentTimeMillis();

                if (numberOfTaps == 3) {
                    Toast.makeText(getApplicationContext(), "triple", Toast.LENGTH_SHORT).show();
                    //handle triple tap
                } else if (numberOfTaps == 2) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //handle double tap
                            Toast.makeText(getApplicationContext(), "double", Toast.LENGTH_SHORT).show();
                        }
                    }, ViewConfiguration.getDoubleTapTimeout());
                }
        }

        return true;
    }
});