在Mapview中单击特定标记后,如何从Firestore数据库中获取活动的特定数据



我创建了一个片段,并在上面获取了一个Mapview。我的Firestore数据库有一个名为Job Post1的集合,其中包含一些文档和字段。通过这个集合,我可以在Mapview上显示的标记的帮助下获取经度和纬度。

我想做的是:在点击Mapview上的标记后,我想在我创建的Bottomsheet上显示该文档(我在经度和纬度的帮助下绘制标记的文档(中的所有字段。

我所做的:我使用documentsnapshot获取字段,在updateBottomSheetContent((方法中,我创建了一个Strings并设置了Text。完成此操作后,当我单击标记时,隐藏的工作表是打开的,并在其上获取他的文档的数据,但当我单击另一个标记时,会获取相同的数据,而不是他自己的文档。这是我的问题。

我的Java代码

package part.time.job.v2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Constraints;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.PlaceLikelihood;
import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest;
import com.google.android.libraries.places.api.net.FindCurrentPlaceResponse;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.maps.android.ui.IconGenerator;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import model.Jobpost;
import model.Journal;
import static com.android.volley.VolleyLog.TAG;

/**
* A simple {@link Fragment} subclass.
*/
public class LabourFragment extends Fragment  implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapView mMapview;

private Location mLastKnownLocation;
Dialog myDialog;
private BottomSheetBehavior bottomSheetBehavior;
private View bottomSheet;
private String title1;
private String desc1;
private String cate1;
private String age1;
private String vac1;
private String loc1;
private String jdate1;
private String time1;
private String whour1;

FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
CollectionReference mOrderRef = mDatabase.collection("Job Post1");
public LabourFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_labour, container, false);
//        myDialog = new Dialog(getActivity());
bottomSheet = view.findViewById(R.id.bottom_sheet);
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheetBehavior.setPeekHeight(120);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Places.initialize(getActivity(), getString(R.string.google_maps_key));
mPlacesClient = Places.createClient(getActivity());

mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());

return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (mGoogleMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mGoogleMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}
@Override
public void onViewCreated(@NonNull View view,  Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapview = (MapView) view.findViewById(R.id.mapView);
if (mMapview!=null){
mMapview.onCreate(null);
mMapview.onResume();
mMapview.getMapAsync(this);
}
}

@Override
public void onMapReady(final GoogleMap googleMap) {
mGoogleMap=googleMap;
FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
CollectionReference mOrderRef = mDatabase.collection("Job Post1");
mOrderRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
if(documentSnapshot.contains("lat") && documentSnapshot.contains("lon")) {
String lat = (String) documentSnapshot.get("lat");
String lon = (String) documentSnapshot.get("lon");
title1= (String) documentSnapshot.get("title");
desc1= (String) documentSnapshot.get("desc");
cate1= (String) documentSnapshot.get("category");
age1= (String) documentSnapshot.get("age");
vac1= (String) documentSnapshot.get("vacancy");
loc1= (String) documentSnapshot.get("location");
jdate1= (String) documentSnapshot.get("jobdate");
time1= (String) documentSnapshot.get("time");
whour1=(String) documentSnapshot.get("workinghours");
if(lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
double latitude = Double.parseDouble(lat.trim());
double longitude = Double.parseDouble(lon.trim());
IconGenerator iconGen = new IconGenerator(getActivity());
googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(title1).icon(BitmapDescriptorFactory.fromBitmap(iconGen.makeIcon(title1))));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 18.0f));
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
marker.getTitle();
updateBottomSheetContent(marker);
return true;
}
});
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
}
}
}

}
});
}
private void updateBottomSheetContent(final Marker marker) {
final TextView jobT = (TextView) bottomSheet.findViewById(R.id.jobTitle);
final TextView desc = (TextView) bottomSheet.findViewById(R.id.jobDesc);
final TextView cate = (TextView) bottomSheet.findViewById(R.id.categ);
final TextView age = (TextView) bottomSheet.findViewById(R.id.ageLimit);
final TextView vac = (TextView) bottomSheet.findViewById(R.id.vacan);
final TextView loc = (TextView) bottomSheet.findViewById(R.id.locat);
final TextView workingH = (TextView) bottomSheet.findViewById(R.id.workingHour);
final TextView date = (TextView) bottomSheet.findViewById(R.id.jobDate);
final TextView expire = (TextView) bottomSheet.findViewById(R.id.jobExpire);
final TextView uid = (TextView) bottomSheet.findViewById(R.id.uid);
final TextView time = (TextView) bottomSheet.findViewById(R.id.timeStart);

jobT.setText(title1);
desc.setText(desc1);
cate.setText(cate1);
age.setText(age1);
vac.setText(vac1);
loc.setText(loc1);
workingH.setText(whour1);
date.setText(jdate1);
time.setText(time1);

bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

}

这是数据库结构

首先需要检查生成的纬度是否存在于数据库字段中。查看官方文件。

Firestore文档

然后,如果存在,你会得到文档id,你可以使用它。

相关内容

  • 没有找到相关文章

最新更新