-1

I had Stored longitude & latitude in Firestore Collection, How can i display location on Map using longitude & latitude

please Guide me.

Following is what I tried :

@Override
public void onMapReady(final GoogleMap googleMap) {
    MapsInitializer.initialize(getContext());
    mGoogleMap = googleMap;

    collectionReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.addMarker(new MarkerOptions().position(new LatLng(18.5204, 73.8567)).title("pune"));
            CameraPosition cameraPosition = CameraPosition.builder().target(new LatLng(18.5204, 73.8567)).zoom(16).bearing(0).tilt(45).build();

        }
    });
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
SHUBHAM SHEDGE
  • 1,695
  • 2
  • 6
  • 16

1 Answers1

1

You have to retrieve latitude and longitude from QuerySnapshot and then need to add this to marker. Check below:

@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));
                    }
                }
            }
        }
    });
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • @shubham-shedge, Check my answer – Md. Asaduzzaman Feb 20 '20 at 12:16
  • E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.part_time_job, PID: 8738 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at com.example.part_time_job.LabourFragment$2.onSuccess(LabourFragment.java:153) – SHUBHAM SHEDGE Feb 20 '20 at 12:32
  • at com.example.part_time_job.LabourFragment$2.onSuccess(LabourFragment.java:145) – SHUBHAM SHEDGE Feb 20 '20 at 12:33
  • Lat and lag are fetch from firestore but when we post new Job then new marker is override with old marker. i want show multiple marker on map activity – SHUBHAM SHEDGE Feb 21 '20 at 04:39
  • @SHUBHAMSHEDGE, that is related to post. You ask question here related to fetch and show from firestore. I think this will resolve using above answer. Please accept the answer and then ask another with your code related to post. Hope you understand. Thanks – Md. Asaduzzaman Feb 21 '20 at 05:42
  • https://stackoverflow.com/questions/60335122/plot-multiple-marker-on-map-view-using-longitude-latitude-from-firestore-cloud please check this Question – SHUBHAM SHEDGE Feb 21 '20 at 09:03