how can handle double click on marker in android a

2019-05-24 14:29发布

问题:

I am displaying a vehicles on a map,when i click on vehicle it shows info window but i need double click on vehicle then it will goes to the new page.I don't know how to do,please help me.here is my code

 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener {

        GoogleMap mMap;
        RequestQueue requestQueue;
        ArrayList<LatLng> points = null;
        PolylineOptions polylineOptions;
        Geocoder geocoder;
        LocationListener locationListener;
        TextView tv,tv_one,tv_two;
        String string;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            points = new ArrayList<LatLng>();
            polylineOptions = new PolylineOptions();
            String URL = "http://b2ss2c.com/gtsapi/index.php?cmd=VEHICLE_DETAILS&deviceID=salim_fiat&accountID=gts&fromDate=23-May-2016&toDate=24-May-2016";
            final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, new Response.Listener<org.json.JSONArray>() {
                @Override
                public void onResponse(org.json.JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {

                        try {
                            JSONObject jsonObject1 = response.getJSONObject(i);
                            String speed=jsonObject1.getString("speed");
                            String deviceID=jsonObject1.getString("deviceID");
                            String altitude=jsonObject1.getString("altitude");
                            String fuelLevel=jsonObject1.getString("fuelLevel");
                            LatLng latLng = new LatLng(jsonObject1.getDouble("latitude"),jsonObject1.getDouble("longitude"));
                            BitmapDescriptor image = BitmapDescriptorFactory.fromResource(R.drawable.car);
                            mMap.setOnMarkerClickListener(MapsActivity.this);
                            Marker marker= mMap.addMarker(new MarkerOptions().position(latLng).icon(image));
                            marker.setTitle("DeviceID:"+deviceID+"\n"+"Speed:"+speed+"\n"+"Altitude:"+altitude+"\n"+"FuelLevel:"+fuelLevel);
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
                            points.add(latLng);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    polylineOptions.addAll(points);
                    polylineOptions.width(4);
                    polylineOptions.color(Color.BLUE);
                    mMap.addPolyline(polylineOptions);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(jsonArrayRequest);
        }
        @Override
        public boolean onMarkerClick(Marker marker) {
            geocoder=new Geocoder(getApplicationContext(), Locale.getDefault());
            if (Geocoder.isPresent()){
                try{
                    List<Address> addresses=geocoder.getFromLocation(marker.getPosition().latitude,marker.getPosition().longitude,1);
                    if (addresses!=null && addresses.size()>0) {
                        android.location.Address address = addresses.get(0);
                        string = String.format("%s %s %s", address.getAddressLine(0),address.getSubLocality(),address.getLocality(),
                                address.getAdminArea(),address.getCountryName());
                        String title=marker.getTitle();
                        marker.setSnippet(title+"\n"+"Address:"+string);
                        marker.showInfoWindow();
                    }else {
                        Toast.makeText(getApplicationContext(),"Address not found",Toast.LENGTH_SHORT).show();
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
            return false;
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    return null;
                }

                @Override
                public View getInfoContents(Marker marker) {
                    View view=getLayoutInflater().inflate(R.layout.info_window,null);
                    tv=(TextView)view.findViewById(R.id.tv_address);
                    tv_one=(TextView)view.findViewById(R.id.tv_one);
                    tv_two=(TextView)view.findViewById(R.id.tv_two);
                    String snippet= marker.getSnippet();
                    tv_one.setText(snippet);
                    return view;
                }
            });

        }


    }

回答1:

Currently you are launching your activity on single click of marker using the onMarkerClick api. In it why dont you use a timer mechanism, which will record the number of clicks. You can do it in following steps

  1. Record time on click & id of marker.
  2. Whenever you get click event on marker. Check if ID is same as previous one and also the time between both events is less than some threshold.
  3. If yes, Launch your activity.
  4. If No, clear the saved id to new one and record time as well.