我正在尝试使用Geofire来存储位置,现在我想在地图上显示已保存的坐标

时间:2017-07-12 11:19:32

标签: android firebase firebase-realtime-database geofire

enter image description here

我正在尝试使用Geofire来存储和显示登录地图的用户我是android studio中的新用户,因为我能够使用geofire在firebase上存储位置 但是我无法从firebase中检索存储的位置

这是我的代码

    public class tracking extends FragmentActivity implements 
  GeoQueryEventListener,OnMapReadyCallback, 
GoogleMap.OnCameraChangeListener{
private static final GeoLocation INITIAL_CENTER = new GeoLocation(30.7333148, 76.7794179);
private static final int INITIAL_ZOOM_LEVEL = 14;
private static final String GEO_FIRE_DB = "https://trace-
5fa8c.firebaseio.com";
private static final String GEO_FIRE_REF = GEO_FIRE_DB + "/_geofire";
private GoogleMap mMap;


private GoogleMap map;
private Circle searchCircle;
private GeoFire geoFire;
private GeoQuery geoQuery;
private Map<String,Marker> markers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tracking);
    // Obtain the SupportMapFragment and get notified when the map is ready 
     to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) 
      getSupportFragmentManager()
            .findFragmentById(R.id.map);
      mapFragment.getMapAsync((OnMapReadyCallback) this);




    // setup GeoFire
    this.geoFire = new GeoFire(FirebaseDatabase.getInstance().getReferenceFromUrl(GEO_FIRE_REF));
    // radius in km
     this.geoQuery = this.geoFire.queryAtLocation(INITIAL_CENTER, 1);

    // setup markers
    this.markers = new HashMap<String, Marker>();
}


@Override
protected void onStop() {
    super.onStop();
    // remove all event listeners to stop updating in the background
    this.geoQuery.removeAllListeners();
    for (Marker marker: this.markers.values()) {
        marker.remove();
    }
    this.markers.clear();
}

@Override
protected void onStart() {
    super.onStart();
    // add an event listener to start updating locations again
    this.geoQuery.addGeoQueryEventListener(this);
}

@Override
public void onKeyEntered(String key, GeoLocation location) {
    // Add a new marker to the map
    Marker marker = this.map.addMarker(new MarkerOptions().position(new 
 LatLng(location.latitude, location.longitude)));
     this.markers.put(key, marker);
 }

 @Override
 public void onKeyExited(String key) {
    // Remove any old marker
    Marker marker = this.markers.get(key);
    if (marker != null) {
        marker.remove();
        this.markers.remove(key);
    }
}

 @Override
 public void onKeyMoved(String key, GeoLocation location) {
    // Move the marker
    Marker marker = this.markers.get(key);
    if (marker != null) {
        this.animateMarkerTo(marker, location.latitude, location.longitude);
    }
 }

 @Override
 public void onGeoQueryReady() {
 }

 @Override
 public void onGeoQueryError(DatabaseError error) {
    new AlertDialog.Builder(this)
            .setTitle("Error")
            .setMessage("There was an unexpected error querying GeoFire: " + 
   error.getMessage())
            .setPositiveButton(android.R.string.ok, null)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
   }

  // Animation handler for old APIs without animation support
  private void animateMarkerTo(final Marker marker, final double lat, final 
  double lng) {
    final Handler handler = new Handler();
     final long start = SystemClock.uptimeMillis();
    final long DURATION_MS = 3000;
    final Interpolator interpolator = new 
    AccelerateDecelerateInterpolator();
    final LatLng startPosition = marker.getPosition();
    handler.post(new Runnable() {
        @Override
        public void run() {
            float elapsed = SystemClock.uptimeMillis() - start;
            float t = elapsed/DURATION_MS;
            float v = interpolator.getInterpolation(t);

            double currentLat = (lat - startPosition.latitude) * v + 
       startPosition.latitude;
            double currentLng = (lng - startPosition.longitude) * v + 
   startPosition.longitude;
            marker.setPosition(new LatLng(currentLat, currentLng));

            // if animation is not finished yet, repeat
            if (t < 1) {
                handler.postDelayed(this, 16);
            }
        }
    });
  }

  private double zoomLevelToRadius(double zoomLevel) {
    // Approximation to fit circle into view
    return 16384000/Math.pow(2, zoomLevel);
}



 @Override
 public void onCameraChange(CameraPosition cameraPosition) {
    LatLng center = cameraPosition.target;
    double radius = zoomLevelToRadius(cameraPosition.zoom);
    this.searchCircle.setCenter(center);
     this.searchCircle.setRadius(radius);
    this.geoQuery.setCenter(new GeoLocation(center.latitude, 
  center.longitude));
    // radius in km
    this.geoQuery.setRadius(radius/1000);
 }

  @Override
  public void onMapReady(GoogleMap googleMap) {
  LatLng latLngCenter = new LatLng(INITIAL_CENTER.latitude, 
    INITIAL_CENTER.longitude);
    this.searchCircle = this.map.addCircle(new 
     CircleOptions().center(latLngCenter).radius(1000));
    this.searchCircle.setFillColor(Color.argb(66, 255, 0, 255));
    this.searchCircle.setStrokeColor(Color.argb(66, 0, 0, 0));
    this.map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngCenter, 
    INITIAL_ZOOM_LEVEL));
    this.map.setOnCameraChangeListener(this);
}
}

我无法检索任何位置坐标(log和lat) 请帮忙 提前谢谢

0 个答案:

没有答案