如何使用geofire从firebase获取位置

Posted

技术标签:

【中文标题】如何使用geofire从firebase获取位置【英文标题】:How to get location from firebase using geofire 【发布时间】:2019-04-11 05:36:39 【问题描述】:

我正在使用 Geofire 获取当前位置并将其保存在 firebase 中。但 id 不知道如何从 firebase 中检索它。我尝试了 *** 上可用的各种代码,但我无法检索它并在其上放置标记。请帮我提供确切的代码。

public class LocationMapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener 

    private GoogleMap mMap;
    private SupportMapFragment mapFragment;
    private DatabaseReference ref;
    private FirebaseUser user;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    LocationRequest mLocationRequest;
    Boolean isLoggingOut=false;
    Button logout1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_maps);
        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

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

            ActivityCompat.requestPermissions(LocationMapsActivity.this,new String[]Manifest.permission.ACCESS_FINE_LOCATION,LOCATION_REQUEST_CODE);

        
        else 

            mapFragment.getMapAsync(this);
        


       /* logout1=(Button)findViewById(R.id.btnLogout);
        logout1.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                isLoggingOut=true;

                disconnectServiceProvider();
                FirebaseAuth.getInstance().signOut();
                Intent intent=new Intent(ServiceProviderMapActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
                return;
            
        );*/
    


    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 

            return;
        
        buildGoogleApiClient();

        mMap.setMyLocationEnabled(true);
        FirebaseAuth firebaseAuth=FirebaseAuth.getInstance();
        String userid=FirebaseAuth.getInstance().getUid();
        user=firebaseAuth.getCurrentUser();
        final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("ServiceProvidersLocations").child(user.getUid());
        final GeoFire geoFire = new GeoFire(dbRef);

        geoFire.getLocation("Locations",new LocationCallback() 
            @Override
            public void onLocationResult(String key, GeoLocation location) 
                LatLng locTION= new LatLng(location.latitude,location.longitude);
                mMap.addMarker(new MarkerOptions().position(locTION).title("Name")).setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));


            

            @Override
            public void onCancelled(DatabaseError databaseError) 

            
        );

     /*   // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
    
    protected synchronized void buildGoogleApiClient()

        mGoogleApiClient=new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();

    

    @Override
    public void onLocationChanged(Location location) 
        mLastLocation=location;
        LatLng latLng=new LatLng(location.getLatitude(),location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker is on your current location"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
        String userId=FirebaseAuth.getInstance().getCurrentUser().getUid();
        ref=FirebaseDatabase.getInstance().getReference("serviceConsumers");

        GeoFire geoFire=new GeoFire(ref);
        geoFire.setLocation(userId,new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()), new GeoFire.CompletionListener() 
            @Override
            public void onComplete(String key, DatabaseError error)
            

            
        );
    

    @Override
    public void onConnected(@Nullable Bundle bundle) 
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            ActivityCompat.requestPermissions(LocationMapsActivity.this,new String[]Manifest.permission.ACCESS_FINE_LOCATION,LOCATION_REQUEST_CODE);

        

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);


    

    @Override
    public void onConnectionSuspended(int i) 


    

    final int LOCATION_REQUEST_CODE=1;
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch(requestCode)
            case LOCATION_REQUEST_CODE: 
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 

                    mapFragment.getMapAsync(this);
                
                else 
                    Toast.makeText(getApplicationContext(),"Please Provide Permission to use application ",Toast.LENGTH_LONG).show();
                
                break;
            





        
    
    private void disconnectServiceProvider()
    
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        String userId=FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference ref=FirebaseDatabase.getInstance().getReference("serviceProvidersAvailable");

        GeoFire geoFire=new GeoFire(ref);

        geoFire.removeLocation(userId);

    

    @Override
    protected void onStop() 
        super.onStop();

        if(!isLoggingOut)
        
            disconnectServiceProvider();

        

    

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 

    

非常感谢。

【问题讨论】:

【参考方案1】:

Check this link,you might be able to get a way of retrieving it...

【讨论】:

但我想从 firebase 中检索它

以上是关于如何使用geofire从firebase获取位置的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Real-time Firebase 检索使用 Geofire 设置的多个位置并在 Android Studio 的地图上显示它们?

Swift iOS Firebase - 如何结合 queryOrdered(byChild) 和 GeoFire observe(.keyEntered) 来同时获取快照和位置结果?

Firebase GeoFire,从我当前位置检索具有特定半径的帖子

如何在没有 Geofire 的情况下将位置对象保存在 Firebase 中?

我想使用 GeoFire 在 android 中填充 Firebase Recycler View

使用 android Geofire 获取经度和纬度