【发布时间】:2017-06-17 13:55:48
【问题描述】:
我正在尝试在我的 android 应用程序中将位置从 firebase 数据库更新为谷歌地图。我已经使用了 firebase 的 onDataChange 方法来做到这一点。我的问题是,即使在 Firebase 数据库中更改了海拔和经度的位置,我仍然无法更新谷歌地图上的位置。下面是 MapsActivity.java 类的代码。
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()与包含 lat/long 值的 java 类一起使用...这将使用gson从您返回的 json 字符串中反序列化(尽管从您的描述中不确定数据是如何的在 db 中结构化)。 -
是的,我确实通过在日志中打印出来确认了这些值。
-
另一个可能的问题可能是
mMap可用的时间(即onMapReady发生在onDataChange回调之后)......尽管如果发生这种情况你应该得到NPE......在任何如果您可能应该等到设置mMap后再进行firebase 查询。 -
我的想法完全一样,但是当我在 Firebase 中更新纬度和经度值时,值的变化应该执行 onDataChange 方法,该方法最终应该将标记位置更改为新的 lat、long 值,但它没有发生.
标签: android google-maps firebase firebase-realtime-database