我正在尝试创建一个从Firebase读取纬度和经度的跟踪器应用,并且还会写入相同的值。在Firebase中,我在RootReference之后有了一个名为" Raunak Trikha"其中有两个孩子" lat"和#34;长"。从这些孩子,我想读取和写入当前位置的值。现在我已成功将Firebase集成到我的android项目中。我无法自己获得价值,所以请帮忙。 请尝试编辑我的代码,因为我是初学者,欢迎任何其他建议。她是我的代码:
MapsActivity.java `
public class MapsActivity extends AppCompatActivity implements
OnMapReadyCallback,ValueEventListener {
private GoogleMap mMap;
private GPS_Tracker gpsTracker;
private Location mLocation;
double latitude, longitude;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mRootReference = firebaseDatabase.getReference();
private DatabaseReference mChildReference = mRootReference.child("Raunak Trikha");
private DatabaseReference mChildReference1 = mChildReference.child("id");
private DatabaseReference mChildReference2 = mChildReference1.child("lat");
private DatabaseReference mChildReference3 = mChildReference1.child("long");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
gpsTracker = new GPS_Tracker(getApplicationContext());
mLocation = gpsTracker.getLocation();
latitude = mLocation.getLatitude();
longitude = mLocation.getLongitude();
// 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);
mChildReference2.setValue(latitude);
mChildReference3.setValue(longitude);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng sydney = new LatLng(latitude, longitude);
//LatLng r=new LatLng(value,rlong);
mMap.addMarker(new MarkerOptions().position(sydney).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.download)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
//mMap.addMarker(new MarkerOptions().position(r).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue(double.class)!=null){
String key=dataSnapshot.getKey();
if(key.equals("lat")){
double rlat=dataSnapshot.getValue(double.class);
}
else if(key.equals("long")){
double rlong=dataSnapshot.getValue(double.class);
}
}
LatLng r=new LatLng(rlat,rlong);
mMap.addMarker(new MarkerOptions().position(r).title("I'm here...").icon(BitmapDescriptorFactory.fromResource(R.mipmap.download)));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
@Override
protected void onStart(){
super.onStart();
mChildReference2.addValueEventListener(this);
mChildReference3.addValueEventListener(this);
}`
答案 0 :(得分:0)
用此
替换你的onDataChange @Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.getValue()!=null){
String key=dataSnapshot.getKey();
if(key.equals("lat")){
String rlat=dataSnapshot.getValue();
}
else if(key.equals("long")){
String rlong=dataSnapshot.getValue();
}
}
LatLng r=new
LatLng(Double.parseDouble(rlat),Double.parseDouble(rlong));
mMap.addMarker(new
MarkerOptions().position(r).title("I'm
here...")
.icon(BitmapDescriptorFactory
.fromResource(R.mipmap.download)));
}
希望有所帮助