2017-06-03 5 views
0

Ich versuche Daten vom Firebase zu bekommen, leider zeigt es stattdessen Marker, es zeigte nur Karte ohne Marker und stürzte ab (Force Close). Können Sie mir die Lösung zeigen, um das zu lösen?Marker mit firebase database zu mapactivity hinzufügen

ist hier mein MapsActivity.java

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 && 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)); 
       } 
      } 
     } 

Und hier ist meine Datenbank in Firebase

Datenbankstruktur

enter image description here

Antwort

0

ich Ihren Code und seine adaequat ausgeführt haben an meinem Ende. Wenn das Problem mit dem Absturz an Ihrem Ende weiterhin besteht, versuchen Sie es einmal mit der Version unten in der App.

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
}) 
compile 'com.android.support:appcompat-v7:25.3.1' 
compile 'com.google.android.gms:play-services-maps:9.2.0' 

compile 'com.google.firebase:firebase-core:9.2.0' 
compile 'com.google.firebase:firebase-database:9.2.0' 
compile 'com.firebase:firebase-client-android:2.3.1' 
compile 'com.google.firebase:firebase-messaging:9.2.0' 
testCompile 'junit:junit:4.12' 
} 
apply plugin: 'com.google.gms.google-services' 
Verwandte Themen