Retrieve saved LatLong value from Firebase and show in marker

只谈情不闲聊 提交于 2019-12-12 03:27:51

问题


I have previously asked the similar question where I had issue with saving the data in the firebase cloud. I managed to store the latitude & longitude data in the firebase database using following code

 @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);
            }
        }
    }

But the problem is when I try to retrieve the location I see is my device's location instead of the saved latlong value in the firebase data base. I have 2 calling activities i.e. 1 is for storing the lat long to firebase and 2 for retrieving it. I can successfully save the value under unique id and update it whereas I am unable to retrieve the latlong. I am using following code for retrieval

        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 );

Here i am changing the lat long value of 1 particular child name "08:9E:08:B4:57:18" manually in database to check if i can see the marker in the map at that location but it just shows my device's current location.

I can provide further screen shot of my database n the application if required. Thanks in advance. Hope to have some valuable pointers.


回答1:


Please add this code after adding marker, so that the marker will move to the location which you added

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);
        } 


来源:https://stackoverflow.com/questions/44860594/retrieve-saved-latlong-value-from-firebase-and-show-in-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!