繁体   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