Android Studio中的Firestore Google Maps Marker setOnClickListerner问题

Posted

技术标签:

【中文标题】Android Studio中的Firestore Google Maps Marker setOnClickListerner问题【英文标题】:Firestore Google Maps Marker setOnClickListerner problem in Android Studio 【发布时间】:2021-08-05 22:12:43 【问题描述】:

你们能帮帮我吗?

当我单击标记时,我的代码显示相同的文档。

我认为我的 for 循环有问题。 我正在创建停车位列表,单击时我将在模态底部表中显示停车位的详细信息 这是我的代码。

public class ParkingSpace extends AppCompatActivity implements OnMapReadyCallback


    private GoogleMap mMap;
    Parking parking;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference parkingRef = db.collection("Parking Lots");


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_space);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);
    

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) 
        mMap = googleMap;
        LatLng sydney = new LatLng(16.4023,120.5960);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,14));
        parkingRef.get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                        for (QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots)
                            parking = documentSnapshot.toObject(Parking.class);
                            parking.setDocumentId(documentSnapshot.getId());

                            String lat = parking.getLatitude();
                            String lon = parking.getLongitude();
                            String title = parking.getparkingName();
                            String owner = parking.getName();
                            String documentId= parking.getDocumentId();
                            double latd = Double.parseDouble(lat);
                            double lond = Double.parseDouble(lon);
                            LatLng location = new LatLng(latd,lond);


                            mMap.addMarker(new MarkerOptions().snippet("Owner: " +owner).position(location).title(title));

                            mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() 
                                @Override
                                public boolean onMarkerClick(@NonNull Marker marker) 
                                    parking = documentSnapshot.toObject(Parking.class);
                                    String path = documentSnapshot.getReference().getPath();

                                    Toast.makeText(ParkingSpace.this, "Document ID:" +documentId, Toast.LENGTH_SHORT).show();
                                    BottomSheet bottomSheet = new BottomSheet();
                                    Bundle bundle = new Bundle();
                                    bundle.putString("lat",lat);
                                    bottomSheet.setArguments(bundle);

                                    bottomSheet.show(getSupportFragmentManager(),"bottomsheet");
                                    return false;
                                
                            );

                        
                    

                );

    



【问题讨论】:

【参考方案1】:

您对所有标记有效地使用相同的 Parking 实例 (parking),因为循环的每次迭代都将其设置为下一个查询结果的值。因此,当标记单击事件发生时,parking 实例是最后一个处理的实例(对于所有标记)。

另一种方法是将parking 实例与新添加的标记相关联,并在onMarkerClick 中获取该实例。

类似这样的:

// ... all your code up to this line remains same ....

// Here the `addMarker` method returns the newly added Marker to which we can associate
// any object needed - so associate the "parking" instance...

Marker m = mMap.addMarker(new MarkerOptions().snippet("Owner: " +owner).position(location).title(title));
m.setTag(parking);

然后在你的onMarkerClick 替换:

parking = documentSnapshot.toObject(Parking.class);

// Use the associated Parking object (saved as a tag) to get needed info
Parking p = (Parking) marker.getTag();
if (p == null) 
   // some other marker on map 
   return;

String documentId = p.getDocumentId();
String lat = p.getLatitude();
// and any other data I may have missed should come from "p".

【讨论】:

以上是关于Android Studio中的Firestore Google Maps Marker setOnClickListerner问题的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Firebase Firestore 无法检索字段数据

在 Android Studio Firestore 中获取查询时出现错误

Android Studio Firestore 时间戳与 currentLocalTime 的比较

如何在 Android Studio Java 中将数组上传到 Firestore 数据

在android studio中引用Firestore

如何在 Android Studio 中使用 Kotlin 从 Firestore 数据库中获取下一条和上一条数据?