试图将2个真人的位置放在地图上

时间:2018-03-05 09:43:40

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

我希望在地图上有2个位置,但它只显示我自己的位置。我希望2个不同的人能够在他们的地图上看到对方。我正在使用firebase,它看起来像这样:

enter image description here

这是我的onMapReady:

    @Override
    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ContextCompat.checkSelfPermission(this, 
Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED)
    {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }

这是我的onLocationChanged:

@Override
public void onLocationChanged(Location location) {
    lastLocation = location;
    if (currentLocationMarker != null)
    {
        currentLocationMarker.remove();
    }
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("current location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

    locations.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .setValue(new Tracking(FirebaseAuth.getInstance().getCurrentUser().getEmail(),
                    FirebaseAuth.getInstance().getCurrentUser().getUid(),
                    String.valueOf(lastLocation.getLatitude()),
                    String.valueOf(lastLocation.getLongitude())));


    currentLocationMarker = mMap.addMarker(markerOptions);


    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
    mMap.animateCamera(cameraUpdate);

    if (client != null)
    {
        LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
    }

}

请帮忙。 我已经尝试放置mapFragments并且它不起作用并且还尝试将此代码放在我的onMapReady中并停止了应用程序:

refDatabase.addChildEventListener(new ChildEventListener() {
       @Override
       public void onChildAdded(DataSnapshot dataSnapshot, String s) {
           LatLng newLocation = new LatLng(
                   dataSnapshot.child("latitude").getValue(Long.class),
                   dataSnapshot.child("longitude").getValue(Long.class)
           );
           mMap.addMarker(new MarkerOptions()
                   .position(newLocation)
                   .title(dataSnapshot.getKey()));

       }

       @Override
       public void onChildChanged(DataSnapshot dataSnapshot, String s) {

       }

       @Override
       public void onChildRemoved(DataSnapshot dataSnapshot) {

       }

       @Override
       public void onChildMoved(DataSnapshot dataSnapshot, String s) {

       }

       @Override
       public void onCancelled(DatabaseError databaseError) {

       }
   });

0 个答案:

没有答案