用户单击mapView上的特定标记时滚动到特定的卡?

时间:2019-07-28 11:13:57

标签: android firebase google-maps android-recyclerview android-mapview

我在mapview上有标记,在recyclerview中有cardview,我想每当用户单击特定标记并将其抬高一点时滚动到特定卡上

我正在从Firebase代码中获取经纬度和lng坐标,以获取坐标 这是代码

获取有关卡数据的功能

  public  void loadhostelList() {

    Query query = null;

    if(!onClickedLocationArea.isEmpty())
    {
        lat.clear();
        lng.clear();
       String q=hostelID+"_"+  locationAreaKeyPair.get(onClickedLocationArea);
        query =databaseReference.orderByChild("LOCATION_AREA").equalTo(q);
    }else{
        query =databaseReference.orderByChild("LOCATION_ID").equalTo(hostelID);

    }

    FirebaseRecyclerOptions<Inside_location> options=
            new FirebaseRecyclerOptions.Builder<Inside_location>()
                    .setQuery(query,Inside_location.class)
                    .setLifecycleOwner(this)
                    .build();


        adapter = new FirebaseRecyclerAdapter<Inside_location, InsideLocatinViewHolder>(options) {
        public long avgRating,sum=0;
        @Override
        protected void onBindViewHolder(@NonNull final InsideLocatinViewHolder holder, int position, @NonNull final Inside_location model) {
           //GET AVG RATING OF HOSTEL
            ratingRef.child(adapter.getRef(position).getKey()).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                        for (DataSnapshot dataSnapshot11 : dataSnapshot1.getChildren()) {
                            sum += (long) dataSnapshot11.getValue();
                        }
                    }

                    if (dataSnapshot.getChildrenCount() == 0) {
                        avgRating = 1;
                    } else {
                        avgRating = (long) sum / dataSnapshot.getChildrenCount();
                    }
                    holder.RATING.setRating(avgRating);
                }
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
            holder.NAME.setText(model.getNAME());
            holder.ADDRESS.setText(model.getADDRESS());
            holder.PRICE.setText("INR "+model.getPRICE()+"*");
            holder.SHARING_TYPE.setText(model.getSHARING_TYPE());
            holder.SEX.setText(model.getSEX());

            Glide.with(insideHostelList.this).load(model.getPICTURES()).apply(new RequestOptions().override(300,185)).into(holder.PICTURES);
            mShimmerViewContainer.stopShimmerAnimation();
            mShimmerViewContainer.setVisibility(View.GONE);
            final Inside_location clickItem = model;
            holder.setItemClickListener(new itemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                  ///  Toast.makeText(insideHostelList.this,""+clickItem.getNAME(),Toast.LENGTH_LONG).show();
                   Intent intent = new Intent(insideHostelList.this,hostelDetails.class);
                  intent.putExtra("insideHostelID",adapter.getRef(position).getKey());
                  startActivity(intent);
                    hostelIdforSelectedRoom = adapter.getRef(position).getKey();
                   double x = (double) model.getDOUBLE_ROOM_AVAILABLE()/2;
                    numberOfDoubleRoom= (int)Math.ceil(x);
                    numberOfSinlgeRoom=model.getSINGLE_ROOM_AVAILABLE();
                    bookedRoomPrice= model.getPRICE();
                    bookedRoomAddress=model.getADDRESS();
                    bookedHostelName = model.getNAME();
                    AvailableRoomTypes=model.getSHARING_TYPE();
                //    Toast.makeText(insideHostelList.this,"db room:"+model.getDOUBLE_ROOM_AVAILABLE(),Toast.LENGTH_LONG).show();
              //      Toast.makeText(insideHostelList.this,"si room:"+model.getSINGLE_ROOM_AVAILABLE(),Toast.LENGTH_LONG).show();

                }
            });
            Toast.makeText(insideHostelList.this, ""+position, Toast.LENGTH_SHORT).show();

            //for animating map marker



        }
        @NonNull
        @Override
        public InsideLocatinViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.insidecardview, viewGroup, false);
            return new InsideLocatinViewHolder(view);
        }
    };


   // Log.d("TAG",""+adapter.getItemCount());
    recyclerView.setAdapter(adapter);

}

//function to get lat and lng 

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for(DataSnapshot childsnap:dataSnapshot.getChildren()){
                LatAndLng latAndLng = childsnap.child("MAP_LOCATION").getValue(LatAndLng.class);
                lat.add(String.valueOf(latAndLng.getLATITUDE()));
                lng.add(String.valueOf(latAndLng.getLONGITUDE() ));
              //  Log.d("latitudess", "onDataChange: "+String.valueOf(latAndLng.getLATITUDE()));
            }
            callMapPins();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


}

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

}

 public  void callMapPins(){

    for (  markerCount=0 ; markerCount<lat.size();markerCount++){
        LatLng sydney = new LatLng(Float.parseFloat(lat.get(markerCount)), Float.parseFloat(lng.get(markerCount)));
        mgoogleMap.addMarker(new MarkerOptions().position(sydney));
        mgoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Log.d("dddddddddd"," "+marker.getPosition().latitude);
                if(lat.contains((String.valueOf(marker.getPosition().latitude)))){
                    Toast.makeText(insideHostelList.this, " I'm present in the list", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(insideHostelList.this, " "+marker.getPosition().latitude, Toast.LENGTH_SHORT).show();


                return false;
            }
        });
        mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,10f));
    }
}

有人可以在这段代码中帮助我,我找到了一个网站,但是它是针对react-native而不是针对android

更新:我已经更新了callMapPins函数,但是当我比较lat和lng时,我遇到的问题来了,我在数据库中存储的corrds末尾的位数更少,并且使用标记获取的coords也不同,所以我无法比较它们是否有解决问题的办法

0 个答案:

没有答案