0

I had Stored longitude & latitude in Firestore Collection and using Marker I Showed it On Map. but only one marker is displayed on a map. I want to show multiple markers on a map using stored longitude & latitude in Firestore Collection.

Here is my java code

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));


                        }
                    }
                }
            }
        });
    }
}

Here I am using mapView and firebase Firestore cloud database

Shweta Shelke
  • 17
  • 1
  • 6

1 Answers1

0

for multiple markers

     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));
    }
Viral Patel
  • 1,296
  • 1
  • 11
  • 24
Durai
  • 59
  • 6
  • longitude & latitude are stored in collection and using collection reference i want to plot multiple markers on map – Shweta Shelke Feb 21 '20 at 09:01
  • now follow this code. which works well for me add multiple markers. – Durai Feb 21 '20 at 09:05
  • try this https://stackoverflow.com/questions/30569854/adding-multiple-markers-in-google-maps-api-v2-android – Durai Feb 21 '20 at 09:08
  • Please read this doc to make for loop to addMarker will genrate: https://developers.google.com/maps/documentation/android-sdk/marker#add_a_marker – Viral Patel Feb 21 '20 at 09:36
  • For demo click this link, it'll static data to plot multiple marker: https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java/com/example/mapdemo/MarkerDemoActivity.java – Viral Patel Feb 21 '20 at 09:38
  • Thank you so much @Durai and Viral – Shweta Shelke Feb 21 '20 at 09:40