【发布时间】:2017-12-05 05:57:17
【问题描述】:
我之前曾问过类似的问题,我在将数据保存在 Firebase 云中时遇到了问题。我设法使用以下代码将纬度和经度数据存储在 firebase 数据库中
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Use a custom info window adapter to handle multiple lines of text in the
// info window contents.
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
// Return null here, so that getInfoContents() is called next.
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// Inflate the layouts for the info window, title and snippet.
View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents,
(FrameLayout)findViewById(R.id.map), false);
TextView title = ((TextView) infoWindow.findViewById(R.id.title));
title.setText(marker.getTitle());
TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet));
snippet.setText(marker.getSnippet());
return infoWindow;
}
});
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
}
// Gets the current location of the device, and positions the map's camera.
private void getDeviceLocation() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = database.getReference();
Intent intent = getIntent();
callingActivity = intent.getIntExtra( "callingActivity",0 );
//If the map has been called from ScanActivity
if (callingActivity == 1) {
// Get the best and most recent location of the device
if (mLocationPermissionGranted) {
mLastKnownLocation = LocationServices.FusedLocationApi
.getLastLocation( mGoogleApiClient );
scanString = intent.getStringArrayListExtra( "beaconList" );
nameList = intent.getStringArrayListExtra( "nameList" );
addressList = intent.getStringArrayListExtra( "addressList" );
RssiList = intent.getIntegerArrayListExtra( "RssiList" );
for ( int i=0; i < scanString.size(); i++) {
addBeacon.name = nameList.get( i );
addBeacon.address = addressList.get( i );
addBeacon.Rssi = RssiList.get( i );
addBeacon.latitude = mLastKnownLocation.getLatitude();
addBeacon.longitude = mLastKnownLocation.getLongitude();
databaseReference.child( "foo" ).child(addBeacon.address).setValue( addBeacon);
}
}
}
但问题是当我尝试检索我看到的位置时,我看到的是我设备的位置,而不是 firebase 数据库中保存的 latlong 值。我有 2 个调用活动,即 1 个用于将 lat long 存储到 firebase,2 个用于检索它。我可以成功地将值保存在唯一 id 下并更新它,而我无法检索 latlong。我正在使用以下代码进行检索
else {
DatabaseReference latlong= FirebaseDatabase.getInstance().getReference().child( "foo" ).child( "08:9E:08:B4:57:18" );
mFirebaseDatabase.keepSynced(true);
al ValueEventListener beaconListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
LatLng newLocation = new LatLng(
dataSnapshot.child("latitude").getValue(Long.class),
dataSnapshot.child("longitude").getValue(Long.class));
//LatLng mRetrieved = new LatLng(dataSnapshot.getValue(beacon.class).latitude, dataSnapshot.getValue(beacon.class).longitude);
//mLastKnownLocation.setLatitude( dataSnapshot.getValue(beacon.class).latitude );
// mLastKnownLocation.setLatitude(60.192059);
mMap.addMarker( new MarkerOptions()
.position( newLocation)
.title( dataSnapshot.getKey()));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseReference.addValueEventListener( beaconListener );
在这里,我在数据库中手动更改 1 个特定子名称“08:9E:08:B4:57:18”的 lat long 值,以检查我是否可以在该位置看到地图中的标记,但它只是显示我设备的当前位置。
如果需要,我可以在应用程序中提供我的数据库的更多屏幕截图。提前致谢。希望能得到一些宝贵的指点。
【问题讨论】:
-
github.com/beaconthesis/beaconFirebaseAndroid/blob/master/… 这是我的应用程序 github 存储库。如果您需要信息。
标签: android google-maps firebase-realtime-database google-maps-markers