从Firebase检索位置,并将标记放在Android的Google Map API上



当按下保存按钮并从firebase检索位置时,我正在尝试创建应用程序以存储位置,并在地图中显示所有引脚。我已经能够将位置保存到具有纬度和经度的位置儿童下的Firebase,但我不知道如何获得值和销钉。我试图按照Firebase在手册上使用它的方式来做到这一点,但它不起作用。谁能帮助解决这个问题?

到目前为止,这是我的代码:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {
private GoogleMap mMap;
private final static int MY_PERMISSION_FINE_LOCATION = 101;
private Button mSaveButton;
private DatabaseReference mDatabase;
private DatabaseReference refDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to 
be used.
    SupportMapFragment mapFragment = (SupportMapFragment) 
    getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    mSaveButton = (Button) findViewById(R.id.Savebtn);
    mDatabase = 
FirebaseDatabase.getInstance().getReference().child("Navigation");
    refDatabase = 
FirebaseDatabase.getInstance().getReference().child("Location");
 }
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng point) {
            // TODO Auto-generated method stub
            // added marker saved as marker and coordinates passed to latlng
            Marker marker = mMap.addMarker(new 
           MarkerOptions().position(point));
            final LatLng latlng = marker.getPosition();
            mSaveButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    DatabaseReference newPost = mDatabase.push();
                    newPost.child("Location").setValue(latlng);
                }
            });
          }
       });
       if (ActivityCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) ==
             PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_FINE_LOCATION);
        }
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_PERMISSION_FINE_LOCATION:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
                        PackageManager.PERMISSION_GRANTED) {
                    mMap.setMyLocationEnabled(true);
                }
            }else {
                Toast.makeText(getApplicationContext(), "This requires location permissions to be granted", Toast
                        .LENGTH_LONG).show();
                finish();
            }
            break;
    }
  }
}

这是将位置保存在firebase

中的方式
Data
  -Kidp45TdInOM3Bsyu2b
         Location
         latitude:19.772613777905196 
         longitude:-9.92741011083126
  -KidsmTExZY2KjnS7S-b
         Location
         latitude: 18.221073689785065
         longitude: -6.573890447616577
  -KidvmAgV0bm2uT_Pcdr
         Location
         latitude: 14.44608051870992
         longitude: -6.510856859385967

首先:您要将结构更改为这样:

 Data
  Location
    -Kidp45TdInOM3Bsyu2b
       latitude:19.772613777905196 
       longitude:-9.92741011083126
    -KidsmTExZY2KjnS7S-b
       latitude: 18.221073689785065
       longitude: -6.573890447616577
    -KidvmAgV0bm2uT_Pcdr
       latitude: 14.44608051870992
       longitude: -6.510856859385967

这样做的方法是:

        mSaveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //change to refDatabase or mDatabase.child("Location")
                DatabaseReference newPost = refDatabase.push();
                //the push() command is already creating unique key
                newPost.setValue(latlng);
            }
        });

和从reddatabase到地图上的标记代码为:

@Override
public void onMapReady(GoogleMap googleMap) {
   mMap = googleMap;
   ...
   refDatabase.addChildEventListener(new ChildEventListener() {
     @Override
     public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
          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 prevChildKey) {}
     @Override
     public void onChildRemoved(DataSnapshot dataSnapshot) {}
     @Override
     public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
     @Override
     public void onCancelled(DatabaseError databaseError) {}
  });
}

最新更新