从Firebase检索已保存的LatLong值并显示在标记中

时间:2017-07-01 11:51:28

标签: android google-maps firebase-realtime-database google-maps-markers

我之前曾问过类似的问题,我在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值,以检查我是否可以在该位置看到地图中的标记,但它只是显示我的设备的当前位置。

如果需要,我可以在应用程序中提供我的数据库的进一步屏幕截图。提前致谢。希望有一些有价值的指示。

1 个答案:

答案 0 :(得分:0)

请在添加标记后添加此代码,以便标记移动到您添加的位置

try {
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
            googleMap.moveCamera(center);
            googleMap.animateCamera(zoom);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }