2017-07-17 3 views
0

So Angezeigt habe ich mehrere Standorte in meiner Firebase Datenbank: Database Structure GeoFire Abfrage alle Marker

Und diese durch alle diese Knoten in einer Schleife geschrieben, extrahieren Sie die Längen- und Breitengrade, und sie dann als Marker angezeigt werden soll.

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("locations"); 
    final GeoFire geoFire = new GeoFire(ref); 

    //Add a location 
    //geoFire.setLocation("meetFar2", new GeoLocation(26.173027, -80.252871)); 

    final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 5.7); 
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() { 
     @Override 
     public void onKeyEntered(String key, GeoLocation location) { 
     System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude)); 
     Query locationQuery = FirebaseDatabase.getInstance().getReference().child("locations"); 
     locationQuery.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot snap : dataSnapshot.getChildren()){ 
       Double latitude = (Double) snap.child("l/0").getValue(); 
       Double longitude = (Double) snap.child("l/1").getValue(); 
       LatLng meetLatLng = new LatLng(latitude, longitude); 
       googleMap.addMarker(new MarkerOptions().position(meetLatLng)); 
      } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
     } 

     @Override 
     public void onKeyExited(String key) { 
     System.out.println(String.format("Key %s is no longer in the search area", key)); 
     } 

     @Override 
     public void onKeyMoved(String key, GeoLocation location) { 
     System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude)); 
     } 

     @Override 
     public void onGeoQueryReady() { 
     } 

     @Override 
     public void onGeoQueryError(DatabaseError error) { 
     Toast.makeText(MainActivity.this, "There was an error with this query " + error, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

Dies alles innerhalb onMapReady getan, nachdem ich den Benutzers letzte bekannte Position (gesetzt als userLocation) bekam. Wenn ich das Programm ausführe, zeigt die Karte jedoch alle Markierungen an, wenn sich mindestens 1 Schlüssel im Abfrage-Radius befindet. Wie kann ich die Ergebnisse der Query nur innerhalb des Radius als Marker anzeigen?

Antwort

0

Es wurde erkannt, dass die anfängliche AbfrageAtLocation-Abfrage die richtige Anzahl an Punkten aus meiner Datenbank zurückgab, sodass ich die zweite Abfrage darin entfernte. Code sieht jetzt so aus:

final GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(userLocation.latitude, userLocation.longitude), 7.0); 
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() { 
     @Override 
     public void onKeyEntered(String key, GeoLocation location) { 
     System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude)); 

     LatLng meetLatLng = new LatLng(location.latitude, location.longitude); 
     googleMap.addMarker(new MarkerOptions().position(meetLatLng)); 
     } 
Verwandte Themen