簡體   English   中英

如何從Firebase數據添加Google地圖標記?

[英]How to add Google Maps marker from Firebase data?

我正在嘗試創建一個從Firebase加載標記的地圖。

我已將位置代碼分為2個不同的代碼:location_left和location_right。

這意味着正常的標記線將如下所示:

 LatLng cod = new LatLng(location_left, location_right);
                googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));

但它不起作用。 應用程序崩潰。

這是代碼:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private static final String FIREBASE_URL="https://haerev.firebaseio.com/";
    private Firebase firebaseRef;
    private GoogleMap mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps2);
        Firebase.setAndroidContext(this);
        firebaseRef = new Firebase(FIREBASE_URL);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.child("users").getChildren()) {
                    String rightLocation = child.child("location_right").getValue().toString();
                    String leftLocation = child.child("location_left").getValue().toString();

                    double location_left = Double.parseDouble(leftLocation);
                    double location_right = Double.parseDouble(rightLocation);
                    String party_title = child.child("party/party_title").getValue().toString();
                    LatLng cod = new LatLng(location_left, location_right);
                    googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
}

刪除此行:

private GoogleMap mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM