朋友标记显示何时从数据库获取

时间:2018-03-14 14:57:27

标签: android firebase gps location

我正在尝试获取其他用户的位置(其他设备,相同的应用)。通过Firebase。我能够确认该位置是否已发送到Firebase。所以问题是加载时。代码不会出现任何错误。请帮我。在这种情况下,P> S Friend and Truck是同样的事情。

MapTracking.java:  代码:

//Add Marker for Truck
LatLng friendLocation = new LatLng(Double.parseDouble(tracking.getLat()),
                               Double.parseDouble(tracking.getLng()));

//Create Location from user coordinates
Location currentUser =new Location("");
currentUser.setLatitude(lat);
currentUser.setLongitude(lng);

//Create coordinates from friend location
Location friend = new Location("");
friend.setLatitude(Double.parseDouble(tracking.getLat()));
friend.setLatitude(Double.parseDouble(tracking.getLng()));

//Add Friend marker on Map
mMap.addMarker(new MarkerOptions()
    .position(friendLocation)
    .title(tracking.getName())
    .snippet("Distancia" +new DecimalFormat("#.#").format(distance(currentUser,friend)))
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lng),12.0f));

ListOnline.java:



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_online);



        //InitView
        listOnline = (RecyclerView)findViewById(R.id.listOnline);
        listOnline.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        listOnline.setLayoutManager(layoutManager);

        //Set toolbar and logout/join main_menu
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolBar);
        toolbar.setTitle("Camiones en servicio");
        setSupportActionBar(toolbar);

        //Firebase
        locations = FirebaseDatabase.getInstance().getReference("Locations");
        onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");
        counterRef = FirebaseDatabase.getInstance().getReference("lastOnline");
        currentUserRef= FirebaseDatabase.getInstance().getReference("lastOnline")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid());//Create new child in lastOnline with key is uid

        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED    )

        {
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            },MY_PERMISSION_REQUEST_CODE);
        }
        else {
            if(checkPlayServices()){
                buildGoogleApiClient();
                createLocationRequest();
                displayLocation();
            }

        }
            setupSystem();


            updateList();








    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode)
        {

            case MY_PERMISSION_REQUEST_CODE:
            {
                if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    if (checkPlayServices())
                    {
                        buildGoogleApiClient();
                        createLocationRequest();
                        displayLocation();
                    }
                }

            }
        }
    }

    private void displayLocation() {
        if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED    )

        {
            return;
        }
        mLastLocation= LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null)
        {
            //Update to Firebase
            locations.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                    .setValue(new Tracking(FirebaseAuth.getInstance().getCurrentUser().getDisplayName(),
                                  FirebaseAuth.getInstance().getCurrentUser().getUid(),
                            String.valueOf(mLastLocation.getLatitude()),
                            String.valueOf(mLastLocation.getLongitude())));





        }
else
        {
            Toast.makeText(this, "No se ha podido obtener la ubicacion", Toast.LENGTH_SHORT).show();
            Log.d("TEST", "No se pudo obtener la ubicacion");
        }
    }

    private void createLocationRequest() {
        mLocationRequest= new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setSmallestDisplacement(DISTANCE);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    private void updateList() {
        adapter= new FirebaseRecyclerAdapter<com.example.luisalonso.trashdriver.User, ListOnlineViewHolder>(
                com.example.luisalonso.trashdriver.User.class,
                R.layout.user_layout,
                ListOnlineViewHolder.class,
                counterRef
        ) {
            @SuppressLint("RestrictedApi")
            @Override
            protected void populateViewHolder(ListOnlineViewHolder viewHolder, final com.example.luisalonso.trashdriver.User model, int position) {
                viewHolder.txtEmail.setText(model.getEmail());

                //We need to implement item click of recycler view
                viewHolder.itemClickListener = new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        // if model is current user, not set click event
                        if(!model.getEmail().equals(FirebaseAuth.getInstance().getCurrentUser().getEmail()))
                        {
                            Intent map = new Intent(ListOnline.this,MapTracking.class);
                            map.putExtra("email",model.getEmail());
                            map.putExtra("lat",mLastLocation.getLatitude());
                            map.putExtra("lng",mLastLocation.getLongitude());
                            startActivity(map);

                        }


                    }
                };


    
&#13;
&#13;
&#13;

0 个答案:

没有答案