2

I'm using the Mapbox Android SDK and want to populate the map with markers whose locations are stored in a Firebase database common to all users of my application. Each marker is stored in a unique record and each record contains the Latitude & Longitude of the marker along with a boolean value indicating if the location has been verified. I have successfully done this using the Mapbox iOS SDK using Annotations. When the Android App is launched I create a feature list of all markers in the database using symbolOptions as shown below inside of the Firebase "childAdded" listener (other listeners are also implemented as part of the ChildEventListener):

        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference markerRef = dbRef.child("markers");
        //load all markers when app starts
        //child added returns all markers, then listens for new additions
        //Log.i(TAG, "Database Reference: " + dbRef);
        List<SymbolOptions> options = new ArrayList<> ();
        markerRef.addChildEventListener ( new ChildEventListener () {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                markerRead = markerRead+1;
                markerKey = dataSnapshot.getKey();
                Log.i(TAG, "Adding Markers: ");
                Log.i(TAG, "Marker Key: " + markerKey);
                x = (Double)dataSnapshot.child ( "x" ).getValue();
                y = (Double) dataSnapshot.child ( "y" ).getValue();
                v = (Boolean) dataSnapshot.child ( "v" ).getValue();
                Log.i(TAG, "Marker Data - x: " + x + " y: " + y + " v: " + v);

                if(v == true) {
                    options.add(new SymbolOptions ()
                            .withLatLng ( new LatLng (y,x))
                            .withIconImage ( "verified" )
                            .withIconAnchor ( "bottom" )
                            .withDraggable ( false )
                    );
                } else {
                    options.add(new SymbolOptions ()
                        .withLatLng ( new LatLng (y,x))
                        .withIconImage ( "unverified" )
                        .withIconAnchor ( "bottom" )
                        .withDraggable ( false )
                    );
                }
                if (markerRead == 1) {
                    symbols = symbolManager.create(options);
                }
            }


The above code does populate the feature list, but since I have no way of knowing when the Firebase "childAdded" function has completed the initial read of the database, the above only adds the first marker.

I have tried to find Mapbox documentation on how to add annotations analogous to the iOS SDK but have been unsuccessful. The tutorial videos have not been updated and use "addMarker" which has been deprecated.

I need to add markers to the map and I also need to assign a unique ID (the "markerKey" in the above code) to each marker so that an individual marker can be changed or deleted in the future. Also the map needs to display new markers that are added and detected by the "child Added" listener. I have not found a way to have additions to the feature list be displayed when added and the symbols created.

Once the markers are initially added to the Map, the App "Listens" to the Firebase database for the following events:

  • A new marker is added
  • An existing marker is moved to a new location
  • An existing marker is deleted

When a marker is moved or deleted the appropriate database listener is notified and passed the record number (key) of the marker ("markerKey" in above code) that was moved (or deleted). If the marker was moved, the record will then contain the new Latitude & Longitude information.

Thanks in advance for the help.

G. Steve
  • 2,739
  • 2
  • 11
  • 17

0 Answers0