MapsActivtiy没有显示来自firebase的多个标记?

时间:2018-02-02 07:00:54

标签: java android google-maps firebase firebase-realtime-database

这一直困扰着我一段时间。出于某种原因,来自firebase的标记未显示在地图中。我一直在寻找一段时间,我几乎在这里查看了不同的回答问题。它或者不适用于我的应用程序,或者它确实不起作用。

我尝试为标记添加静态,硬编码的latlongs。添加了一堆,应用程序符合。它显示了所有静态的硬编码标记。不幸的是,与firebase调用循环代码一起创建标记仍然没有显示。

MapsActivity.java

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnMyLocationClickListener, GoogleMap.OnInfoWindowClickListener {

private GoogleMap mMap;
private UiSettings mUiSettings;

static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

private DatabaseReference mBlog;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

Marker marker;
List<Blogzone> mapList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    initDrawer();

    mapList = new ArrayList<Marker>();
    mBlog = FirebaseDatabase.getInstance().getReference().child("Blogzone");
    mBlog.push().setValue(marker);


    //Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (mAuth.getCurrentUser() == null) {
                Intent loginIntent = new Intent(MapsActivity.this, LoginActivity.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(loginIntent);
            }
        }
    };
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mAuth.addAuthStateListener(mAuthListener);
    mMap = googleMap;
    mUiSettings = mMap.getUiSettings();


    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) {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

        return;
    }

    mMap.setMyLocationEnabled(true);

    //  UI Settings state
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setMyLocationButtonEnabled(true);

    //Post Markers
    mMap.setOnMarkerClickListener(this);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setOnInfoWindowClickListener(this);
    mMap.clear();

    final LatLng PH = new LatLng(16.566233,121.262634);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PH, 6));


    mBlog.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot s : dataSnapshot.getChildren()){

                Blogzone here = s.getValue(Blogzone.class);

                double latitude = Double.parseDouble(here.latitude);
                double longitude = Double.parseDouble(here.longitude);

                mapList.add(here);

                for (int i = 0; i < mapList.size(); i++)
                {
                    LatLng LOC = new LatLng(latitude,longitude);
                    if (mMap != null) {
                        Log.d("the locations", "Get location: " + latitude + ", " + longitude + " at " + here.title);
                        marker = mMap.addMarker(new MarkerOptions().position(LOC).title(here.title));
                    }
                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Blogzone.class

public class Blogzone {

public String title, desc, imageUrl, username;
public String latitude, longitude;

public Blogzone(String title, String desc, String imageUrl, String username, String latitude, String longitude) {
    this.title = title;
    this.desc = desc;
    this.imageUrl=imageUrl;
    this.username = username;
    this.latitude = latitude;
    this.longitude = longitude;
}

public Blogzone() {
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public void setUsername(String username) {
    this.username = username;
}

public void setTitle(String title) {
    this.title = title;
}

public void setDesc(String desc) { this.desc = desc; }

public void setlatitude(String latitude) { this.latitude = latitude; }

public void setLongitude(String longitude) { this.longitude = longitude; }

public String getUsername() {
    return username;
}

public String getImageUrl() {
    return imageUrl;
}

public String getTitle() {
    return title;
}

public String getDesc() {
    return desc;
}

public String getLatitude() {
    return latitude;
}
public String getLongitude() {
    return longitude;
}

}

编辑: 这是日志。 there are some null string error but I think it doesn't affect the problem

请注意,这只是一个项目应用程序,我刚从Web上的不同教程和演示中学习。我刚刚开始编写Java去年。

我希望有人能尽快帮助我。

0 个答案:

没有答案