title: Add marker to mapsactivity with firebase database
body: <p>I'm trying to retrieve data from firebase, unfortunately instead off showing markers, it just showed map without markers and crashed(Force Close). Can you show me the solution to solve this ?</p>
  
  <p>Here is my MapsActivity.java</p>
  
  <pre><code>package com.example.rizkafs.laundrize;
  
          import android.content.Context;
          import android.content.pm.PackageManager;
          import android.content.res.Resources;
          import android.location.Criteria;
          import android.location.Location;
          import android.location.LocationManager;
          import android.support.annotation.NonNull;
          import android.support.annotation.Nullable;
          import android.support.v4.app.ActivityCompat;
          import android.support.v4.app.FragmentActivity;
          import android.os.Bundle;
          import android.util.Log;
  
          import com.google.android.gms.common.ConnectionResult;
          import com.google.android.gms.common.api.GoogleApiClient;
          import com.google.android.gms.maps.CameraUpdateFactory;
          import com.google.android.gms.maps.GoogleMap;
          import com.google.android.gms.maps.OnMapReadyCallback;
          import com.google.android.gms.maps.SupportMapFragment;
          import com.google.android.gms.maps.model.BitmapDescriptor;
          import com.google.android.gms.maps.model.BitmapDescriptorFactory;
          import com.google.android.gms.maps.model.CameraPosition;
          import com.google.android.gms.maps.model.LatLng;
          import com.google.android.gms.maps.model.MapStyleOptions;
          import com.google.android.gms.maps.model.Marker;
          import com.google.android.gms.maps.model.MarkerOptions;
          import com.google.firebase.database.DataSnapshot;
          import com.google.firebase.database.DatabaseError;
          import com.google.firebase.database.DatabaseReference;
          import com.google.firebase.database.FirebaseDatabase;
          import com.google.firebase.database.ValueEventListener;
  
          import java.util.ArrayList;
  
  
          public class NearbyActivity extends FragmentActivity implements OnMapReadyCallback {
  
              private static final String TAG = NearbyActivity.class.getSimpleName();
              private GoogleMap mMap;
  
              Double latitude;
              Double longitude;
              String namaTempat;
              String alamat;
              String jenis;
              String harga;
              String jamOperasi;
              String noTelp;
  
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_nearby);
                  // 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(GoogleMap googleMap) {
  
                  mMap = googleMap;
  
                  try {
                      // Customise the styling of the base map using a JSON object defined
                      // in a raw resource file.
                      boolean success = googleMap.setMapStyle(
                              MapStyleOptions.loadRawResourceStyle(
                                      this, R.raw.style_json));
  
                      if (!success) {
                          Log.e(TAG, "Style parsing failed.");
                      }
                  } catch (Resources.NotFoundException e) {
                      Log.e(TAG, "Can't find style. Error: ", e);
                  }
  
                  DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
  
                  myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          for (DataSnapshot child : dataSnapshot.child("places").getChildren()){
                              latitude = child.child("latitude").getValue(Double.class);
                              longitude = child.child("longitude").getValue(Double.class);
                              namaTempat = child.child("nama_tempat").getValue(String.class);
                              alamat = child.child("alamat").getValue(String.class);
                              jenis = child.child("jenis").getValue(String.class);
                              harga = child.child("harga").getValue(String.class);
                              jamOperasi = child.child("jam_operasional").getValue(String.class);
                              noTelp = child.child("no_telp").getValue(String.class);
  
                              LatLng latLng = new LatLng(latitude, longitude);
                              mMap.addMarker(new MarkerOptions()
                                      .position(latLng)
                                      .title(namaTempat)
                                      .icon(BitmapDescriptorFactory.fromResource(R.drawable.laundry)));
                          }
  
  
                      }
  
                      @Override
                      public void onCancelled(DatabaseError databaseError) {
  
                      }
                  });
  
                  LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
                  Criteria criteria = new Criteria();
  
                  if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      // TODO: Consider calling
                      //    ActivityCompat#requestPermissions
                      // here to request the missing permissions, and then overriding
                      //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                      //                                          int[] grantResults)
                      // to handle the case where the user grants the permission. See the documentation
                      // for ActivityCompat#requestPermissions for more details.
                      return;
                  }
                  Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
                  if(location != null) {
                      CameraPosition cameraPosition = new CameraPosition.Builder()
                              .target(new LatLng(location.getLatitude(), location.getLongitude()))
                              .zoom(17)
                              .build();
                      mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                  }
              }
          }
  </code></pre>
  
  <p>And here is my database in Firebase</p>
  
  <p><strong>Database Structure</strong></p>
  
  <p><a href="https://i.stack.imgur.com/lO7Sz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lO7Sz.png" alt="enter image description here"></a></p>