[英]How to add Google Maps marker from Firebase data?
I'm trying to create a map that loads markers from Firebase. 我正在尝试创建一个从Firebase加载标记的地图。
I've divided the position code to 2 different code: location_left and location_right. 我已将位置代码分为2个不同的代码:location_left和location_right。
It means that a normal Marker line will look like this: 这意味着正常的标记线将如下所示:
LatLng cod = new LatLng(location_left, location_right);
googleMap.addMarker(new MarkerOptions().position(cod).title(party_title));
But it doesn't work. 但它不起作用。 The application crashes. 应用程序崩溃。
This is the code: 这是代码:
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.