简体   繁体   中英

I want to get latitude and longitude from Cloud Firestore database to add markers on map Android Studio

So I have 3 fields in a Cloud Firestore collection and I want to get these details in order to add markers on my map (make a LatLng from placeLa t and placeLng and name of marker position from placeName).

image here

I also have a file that have get and set methods for the fields.

public class ListPlacesModel implements Serializable {

String name;
double lat;
double lng;
String documentId;

public ListPlacesModel() {
}

public ListPlacesModel(String name, double lat, double lng) {
    this.name = name;
    this.lat = lat;
    this.lng = lng;
}

public String getDocumentId() {
    return documentId;
}

public void setDocumentId(String documentId) {
    this.documentId = documentId;
}

public String getPlaceName() {
    return name;
}

public void setPlaceName(String name) {
    this.name = name;
}

public double getPlaceLat() {
    return lat;
}

public void setPlaceLat(double lat) {
    this.lat = lat;
}

public double getPlaceLng() {
    return lng;
}

public void setPlaceLng(double lng) {
    this.lng = lng;
}

I tried to do this in onMapReady method of my MapActivity, but I can't figure out why it doesn't show anything. It didn't put any marker and didn't show any error either.

ListPlacesModel model = new ListPlacesModel();

    db.collection("AddPlaces")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            LatLng place = new LatLng(model.getPlaceLat(), model.getPlaceLng());
                            gMap.addMarker(new MarkerOptions().position(place));
                        }
                        
                    } else {
                        Log.w("EROARE", "Error getting documents.", task.getException());
                    }
                }
            });

Any ideas why this didn't work or how I can do what I'm trying to do?

The problem in your code lies in the fact the names of the fields inside the class are different than the ones in the database. In your class, you have a field called lat , while in the database is called placeLat , and that's not correct. The field names must be the same. So in your case, the simplest solution would be to change the names of the fields in the class to match the ones in the database.

Alternatively, you use an annotation in front of the getters like this:

@PropertyName("placeLat")
public String getPlaceLat() {
    return lat;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM