为我的标记创建模型:
public class FirebaseMarker {
public String dob;
public String dod;
public String name;
public double latitude;
public double longitude;
public FirebaseMarker() {
}
public FirebaseMarker(String name, double latitude, double longitude, String dob, String dod) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.dob = dob;
this.dod = dod;
}
同时为所有这些参数创建getter和setter,在此之后我创建了MapActivity类(这个活动我在我的recyclerView中按下项目时打开)
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
ChildEventListener mChildEventListener;
DatabaseReference mProfileRef = FirebaseDatabase.getInstance().getReference("Profile");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
createMapView();
FirebaseMarker marker = new FirebaseMarker("Lincoln", -34.506081, 150.88104, "24/12/1940", "02/07/2016");
FirebaseMarker marker2 = new FirebaseMarker("Lincoln", -35.506081, 140.88104, "24/12/1940", "02/07/2016");
mProfileRef.push().setValue(marker);
mProfileRef.push().setValue(marker2);
}
private void createMapView() {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
LatLng wollongong = new LatLng(-34.404336, 150.881632);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(wollongong, 18));
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
addMarkersToMap(googleMap);
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) {
return;
}
googleMap.setMyLocationEnabled(true);
}
@Override
public void onStop() {
if (mChildEventListener != null)
mProfileRef.removeEventListener(mChildEventListener);
super.onStop();
}
private void addMarkersToMap(GoogleMap googleMap) {
mChildEventListener = mProfileRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
FirebaseMarker marker = dataSnapshot.getValue(FirebaseMarker.class);
double latitude = marker.getLatitude();
double longitude = marker.getLongitude();
String name = marker.getName();
LatLng location = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(location).title(name));
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
但是当我打开活动时,我只看到一张地图,我怎么能看到标记?
答案 0 :(得分:0)
您定义变量
GoogleMap googleMap;
并在
public void onMapReady(GoogleMap googleMap)
他们是一样的 那么尝试重命名呢
所以
GoogleMap MgoogleMap;
并编辑 onMapReady
public void onMapReady(GoogleMap googleMap) {
MgoogleMap =googleMap
addMarkersToMap(MgoogleMap );
}