我正嘗試將位置表單Firebase數據庫更新爲我的Android應用程序中的Google地圖。我已經使用firebase的onDataChange方法來實現這一點。我的問題是,即使Firebase數據庫中的高度和經度位置已更改,我仍無法更新Google地圖上的位置。以下是我的MapsActivity.java類的代碼。無法使用Firebase更新Google地圖位置
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
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.LatLng;
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;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Location");
// myRef.setValue("Hello, World!");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
value = dataSnapshot.getValue(String.class);
String [] separated = value.split(",");
String latiPos= separated[0].trim();
String longiPos =separated[1].trim();
String TAG ="VAL";
double dLat = Double.parseDouble(latiPos);
double dLong = Double.parseDouble(longiPos);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(dLat, dLong);
mMap.addMarker(new MarkerOptions().position(sydney).title(latiPos+" "+longiPos));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title(value));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
您是否確認調用了'onDataChange'並且它正確解析經緯度值?可能與您的問題沒有關係,但會建議您使用調用'dataSnapshot.getValue()'與包含緯度/長度值的java類...它將使用'gson'從您返回的json字符串中反序列化(雖然從您的描述數據如何在db中結構化)。 –
是的,我通過在日誌中打印出來確認了這些值。 –
另一個可能的問題可能出現在'mMap'可用的時間點(即'onMapChange'回調後發生'onMapReady')...儘管如果發生這種情況,您應該得到NPE ......無論如何,您應該等待您的Firebase查詢,直到已設置「mMap」。 –