从Firestore Cloud数据库在地图视图上绘制多个标记?

问题描述 投票:0回答:1

我已将经度和纬度存储在Firestore集合中,并使用标记将其显示在地图上。但地图上仅显示一个标记。我想使用Firestore Collection中存储的经度和纬度在地图上显示多个标记。

这是我的Java代码

package part.time.job.v2;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

public class LabourFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    MapView mMapview;
    public LabourFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_labour, container, false);

        return view;
    }

    @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) {
        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");
                        String title = (String) documentSnapshot.get("title");

                        if (lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
                            double latitude = Double.parseDouble(lat.trim());
                            double longitude = Double.parseDouble(lon.trim());
                            LatLng latLng = new LatLng(latitude, longitude);
                            googleMap.addMarker(new MarkerOptions().position(latLng).title(title));
                            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


                        }
                    }
                }
            }
        });
    }
}

这里我正在使用mapView和Firebase Firestore云数据库

android google-cloud-firestore google-maps-markers android-mapview
1个回答
0
投票

用于多个标记

     if(lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
         double latitude = Double.parseDouble(lat.trim());
         double longitude = Double.parseDouble(lon.trim());
         addmarker(latitude,longitude );
     }
     private void addmarker(Float latitude, Float longitude) {
          mgoogleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
          mgoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
    }
© www.soinside.com 2019 - 2024. All rights reserved.