com.firebase.geofire.GeoLocation Java Examples

The following examples show how to use com.firebase.geofire.GeoLocation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DriverHome.java    From UberClone with MIT License 6 votes vote down vote up
private void displayLocation(){
    if (Common.currentLat!=null && Common.currentLng!=null){
        if (locationSwitch.isChecked()) {
            geoFire.setLocation(Common.userID,
                    new GeoLocation(Common.currentLat, Common.currentLng),
                    new GeoFire.CompletionListener() {
                        @Override
                        public void onComplete(String key, DatabaseError error) {
                            LatLng currentLocation = new LatLng(Common.currentLat, Common.currentLng);
                            if (currentLocationMarket != null) currentLocationMarket.remove();

                            currentLocationMarket = mMap.addMarker(new MarkerOptions().position(currentLocation)
                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marker))
                                    .title("Your Location"));
                            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Common.currentLat, Common.currentLng), 15.0f));

                        }
                    });
        }
    }else{
        Message.messageError(this, Errors.WITHOUT_LOCATION);
    }

}
 
Example #2
Source File: DriverTracking.java    From UberClone with MIT License 6 votes vote down vote up
private void displayLocation(){
    //add driver location
    if(driverMarker!=null)driverMarker.remove();
    driverMarker=mMap.addMarker(new MarkerOptions().position(new LatLng(Common.currentLat, Common.currentLng))
    .title("You").icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_marker)));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Common.currentLat, Common.currentLng), 14f));
    geoFire.setLocation(Common.userID,
            new GeoLocation(Common.currentLat, Common.currentLng),
            new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {

                }
            });
    //remove route
    if(direction!=null)direction.remove();
      getDirection();

}
 
Example #3
Source File: Home.java    From UberClone with MIT License 6 votes vote down vote up
private void requestPickup(String uid) {
    DatabaseReference dbRequest=FirebaseDatabase.getInstance().getReference(Common.pickup_request_tbl);
    GeoFire mGeofire=new GeoFire(dbRequest);
    mGeofire.setLocation(uid, new GeoLocation(Common.currenLocation.latitude, Common.currenLocation.longitude),
            new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {

                }
            });
    if (riderMarket.isVisible())riderMarket.remove();
    riderMarket=mMap.addMarker(new MarkerOptions().title(getResources().getString(R.string.pickup_here)).snippet("").position(new LatLng(Common.currenLocation.latitude, Common.currenLocation.longitude))
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    riderMarket.showInfoWindow();
    btnRequestPickup.setText(getResources().getString(R.string.getting_uber));
    findDriver();
}
 
Example #4
Source File: GeoHashQuery.java    From geofire-java with MIT License 5 votes vote down vote up
public static int bitsForBoundingBox(GeoLocation location, double size) {
    double latitudeDegreesDelta = GeoUtils.distanceToLatitudeDegrees(size);
    double latitudeNorth = Math.min(90, location.latitude + latitudeDegreesDelta);
    double latitudeSouth = Math.max(-90, location.latitude - latitudeDegreesDelta);
    int bitsLatitude = (int)Math.floor(Utils.bitsLatitude(size)) *2;
    int bitsLongitudeNorth = (int)Math.floor(Utils.bitsLongitude(size, latitudeNorth)) *2 - 1;
    int bitsLongitudeSouth = (int)Math.floor(Utils.bitsLongitude(size, latitudeSouth)) *2 - 1;
    return Math.min(bitsLatitude, Math.min(bitsLongitudeNorth, bitsLongitudeSouth));
}
 
Example #5
Source File: TestCallback.java    From geofire-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onLocationResult(String key, GeoLocation location) {
    if (future.isDone()) {
        throw new IllegalStateException("Already received callback");
    }

    if (location != null) {
        future.put(location(key, location.latitude, location.longitude));
    } else {
        future.put(noLocation(key));
    }
}
 
Example #6
Source File: TestCallback.java    From geofire-java with MIT License 5 votes vote down vote up
@Override
public void onLocationResult(String key, GeoLocation location) {
    if (future.isDone()) {
        throw new IllegalStateException("Already received callback");
    }

    if (location != null) {
        future.put(location(key, location.latitude, location.longitude));
    } else {
        future.put(noLocation(key));
    }
}
 
Example #7
Source File: GeoHashQuery.java    From geofire-android with Apache License 2.0 5 votes vote down vote up
public static int bitsForBoundingBox(GeoLocation location, double size) {
    double latitudeDegreesDelta = GeoUtils.distanceToLatitudeDegrees(size);
    double latitudeNorth = Math.min(90, location.latitude + latitudeDegreesDelta);
    double latitudeSouth = Math.max(-90, location.latitude - latitudeDegreesDelta);
    int bitsLatitude = (int)Math.floor(Utils.bitsLatitude(size)) *2;
    int bitsLongitudeNorth = (int)Math.floor(Utils.bitsLongitude(size, latitudeNorth)) *2 - 1;
    int bitsLongitudeSouth = (int)Math.floor(Utils.bitsLongitude(size, latitudeSouth)) *2 - 1;
    return Math.min(bitsLatitude, Math.min(bitsLongitudeNorth, bitsLongitudeSouth));
}
 
Example #8
Source File: GeoHash.java    From geofire-java with MIT License 5 votes vote down vote up
public GeoHash(double latitude, double longitude, int precision) {
    if (precision < 1) {
        throw new IllegalArgumentException("Precision of GeoHash must be larger than zero!");
    }
    if (precision > MAX_PRECISION) {
        throw new IllegalArgumentException("Precision of a GeoHash must be less than " + (MAX_PRECISION + 1) + "!");
    }
    if (!GeoLocation.coordinatesValid(latitude, longitude)) {
        throw new IllegalArgumentException(String.format(US, "Not valid location coordinates: [%f, %f]", latitude, longitude));
    }
    double[] longitudeRange = { -180, 180 };
    double[] latitudeRange = { -90, 90 };

    char[] buffer = new char[precision];

    for (int i = 0; i < precision; i++) {
        int hashValue = 0;
        for (int j = 0; j < Base32Utils.BITS_PER_BASE32_CHAR; j++) {
            boolean even = (((i*Base32Utils.BITS_PER_BASE32_CHAR) + j) % 2) == 0;
            double val = even ? longitude : latitude;
            double[] range = even ? longitudeRange : latitudeRange;
            double mid = (range[0] + range[1])/2;
            if (val > mid) {
                hashValue = (hashValue << 1) + 1;
                range[0] = mid;
            } else {
                hashValue = hashValue << 1;
                range[1] = mid;
            }
        }
        buffer[i] = Base32Utils.valueToBase32Char(hashValue);
    }
    this.geoHash = new String(buffer);
}
 
Example #9
Source File: DriverMapActivity.java    From UberClone with MIT License 5 votes vote down vote up
@Override
public void onLocationResult(LocationResult locationResult) {
    for(Location location : locationResult.getLocations()){
        if(getApplicationContext()!=null){

            if(!customerId.equals("") && mLastLocation!=null && location != null){
                rideDistance += mLastLocation.distanceTo(location)/1000;
            }
            mLastLocation = location;


            LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

            String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
            DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
            DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");
            GeoFire geoFireAvailable = new GeoFire(refAvailable);
            GeoFire geoFireWorking = new GeoFire(refWorking);

            switch (customerId){
                case "":
                    geoFireWorking.removeLocation(userId);
                    geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
                    break;

                default:
                    geoFireAvailable.removeLocation(userId);
                    geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
                    break;
            }
        }
    }
}
 
Example #10
Source File: GeoHash.java    From geofire-android with Apache License 2.0 5 votes vote down vote up
public GeoHash(double latitude, double longitude, int precision) {
    if (precision < 1) {
        throw new IllegalArgumentException("Precision of GeoHash must be larger than zero!");
    }
    if (precision > MAX_PRECISION) {
        throw new IllegalArgumentException("Precision of a GeoHash must be less than " + (MAX_PRECISION + 1) + "!");
    }
    if (!GeoLocation.coordinatesValid(latitude, longitude)) {
        throw new IllegalArgumentException(String.format(US, "Not valid location coordinates: [%f, %f]", latitude, longitude));
    }
    double[] longitudeRange = { -180, 180 };
    double[] latitudeRange = { -90, 90 };

    char[] buffer = new char[precision];

    for (int i = 0; i < precision; i++) {
        int hashValue = 0;
        for (int j = 0; j < Base32Utils.BITS_PER_BASE32_CHAR; j++) {
            boolean even = (((i*Base32Utils.BITS_PER_BASE32_CHAR) + j) % 2) == 0;
            double val = even ? longitude : latitude;
            double[] range = even ? longitudeRange : latitudeRange;
            double mid = (range[0] + range[1])/2;
            if (val > mid) {
                hashValue = (hashValue << 1) + 1;
                range[0] = mid;
            } else {
                hashValue = hashValue << 1;
                range[1] = mid;
            }
        }
        buffer[i] = Base32Utils.valueToBase32Char(hashValue);
    }
    this.geoHash = new String(buffer);
}
 
Example #11
Source File: PlaceGeoQueryEventListener.java    From android-rxgeofence with MIT License 4 votes vote down vote up
@Override public void onKeyMoved(String key, GeoLocation location) {
  onChange(GeoFenceType.MOVE, key, place);
}
 
Example #12
Source File: LatLng.java    From android-rxgeofence with MIT License 4 votes vote down vote up
public GeoLocation toGeoLocation() {
  return new GeoLocation(latitude, longitude);
}
 
Example #13
Source File: GeoUtils.java    From geofire-java with MIT License 4 votes vote down vote up
public static double distance(GeoLocation location1, GeoLocation location2) {
    return distance(location1.latitude, location1.longitude, location2.latitude, location2.longitude);
}
 
Example #14
Source File: GeoHashQuery.java    From geofire-java with MIT License 4 votes vote down vote up
public static Set<GeoHashQuery> queriesAtLocation(GeoLocation location, double radius) {
    int queryBits = Math.max(1, Utils.bitsForBoundingBox(location, radius));
    int geoHashPrecision = (int) Math.ceil((float)queryBits /Base32Utils.BITS_PER_BASE32_CHAR);

    double latitude = location.latitude;
    double longitude = location.longitude;
    double latitudeDegrees = radius/Constants.METERS_PER_DEGREE_LATITUDE;
    double latitudeNorth = Math.min(90, latitude + latitudeDegrees);
    double latitudeSouth = Math.max(-90, latitude - latitudeDegrees);
    double longitudeDeltaNorth = GeoUtils.distanceToLongitudeDegrees(radius, latitudeNorth);
    double longitudeDeltaSouth = GeoUtils.distanceToLongitudeDegrees(radius, latitudeSouth);
    double longitudeDelta = Math.max(longitudeDeltaNorth, longitudeDeltaSouth);

    Set<GeoHashQuery> queries = new HashSet<>();

    GeoHash geoHash = new GeoHash(latitude, longitude, geoHashPrecision);
    GeoHash geoHashW = new GeoHash(latitude, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashE = new GeoHash(latitude, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    GeoHash geoHashN = new GeoHash(latitudeNorth, longitude, geoHashPrecision);
    GeoHash geoHashNW = new GeoHash(latitudeNorth, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashNE = new GeoHash(latitudeNorth, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    GeoHash geoHashS = new GeoHash(latitudeSouth, longitude, geoHashPrecision);
    GeoHash geoHashSW = new GeoHash(latitudeSouth, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashSE = new GeoHash(latitudeSouth, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    queries.add(queryForGeoHash(geoHash, queryBits));
    queries.add(queryForGeoHash(geoHashE, queryBits));
    queries.add(queryForGeoHash(geoHashW, queryBits));
    queries.add(queryForGeoHash(geoHashN, queryBits));
    queries.add(queryForGeoHash(geoHashNE, queryBits));
    queries.add(queryForGeoHash(geoHashNW, queryBits));
    queries.add(queryForGeoHash(geoHashS, queryBits));
    queries.add(queryForGeoHash(geoHashSE, queryBits));
    queries.add(queryForGeoHash(geoHashSW, queryBits));

    // Join queries
    boolean didJoin;
    do {
        GeoHashQuery query1 = null;
        GeoHashQuery query2 = null;
        for (GeoHashQuery query: queries) {
            for (GeoHashQuery other: queries) {
                if (query != other && query.canJoinWith(other)) {
                    query1 = query;
                    query2 = other;
                    break;
                }
            }
        }
        if (query1 != null && query2 != null) {
            queries.remove(query1);
            queries.remove(query2);
            queries.add(query1.joinWith(query2));
            didJoin = true;
        } else {
            didJoin = false;
        }
    } while (didJoin);

    return queries;
}
 
Example #15
Source File: GeoHash.java    From geofire-java with MIT License 4 votes vote down vote up
public GeoHash(GeoLocation location) {
    this(location.latitude, location.longitude, DEFAULT_PRECISION);
}
 
Example #16
Source File: GeoQueryDataEventTestListener.java    From geofire-java with MIT License 4 votes vote down vote up
@Override
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
  if (recordEntered) {
    addEvent(dataEntered(dataSnapshot.getKey(), location.latitude, location.longitude));
  }
}
 
Example #17
Source File: GeoQueryDataEventTestListener.java    From geofire-java with MIT License 4 votes vote down vote up
@Override
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
  if (recordMoved) {
    addEvent(dataMoved(dataSnapshot.getKey(), location.latitude, location.longitude));
  }
}
 
Example #18
Source File: GeoQueryDataEventTestListener.java    From geofire-java with MIT License 4 votes vote down vote up
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
  if (recordChanged) {
    addEvent(dataChanged(dataSnapshot.getKey(), location.latitude, location.longitude));
  }
}
 
Example #19
Source File: GeoQueryEventTestListener.java    From geofire-java with MIT License 4 votes vote down vote up
@Override
public void onKeyEntered(String key, GeoLocation location) {
    if (recordEntered) {
        addEvent(keyEntered(key, location.latitude, location.longitude));
    }
}
 
Example #20
Source File: GeoQueryEventTestListener.java    From geofire-java with MIT License 4 votes vote down vote up
@Override
public void onKeyMoved(String key, GeoLocation location) {
    if (recordMoved) {
        addEvent(keyMoved(key, location.latitude, location.longitude));
    }
}
 
Example #21
Source File: PlaceGeoQueryEventListener.java    From android-rxgeofence with MIT License 4 votes vote down vote up
@Override public void onKeyEntered(String key, GeoLocation location) {
  onChange(GeoFenceType.ENTER, key, place);
}
 
Example #22
Source File: AbsGeoQueryEventListener.java    From android-rxgeofence with MIT License 4 votes vote down vote up
@Override public void onKeyEntered(String key, GeoLocation location) {
}
 
Example #23
Source File: AbsGeoQueryEventListener.java    From android-rxgeofence with MIT License 4 votes vote down vote up
@Override public void onKeyMoved(String key, GeoLocation location) {
}
 
Example #24
Source File: Place.java    From android-rxgeofence with MIT License 4 votes vote down vote up
public GeoLocation toGeoLocation() {
  return new GeoLocation(lat, lng);
}
 
Example #25
Source File: GeoHash.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
public GeoHash(GeoLocation location) {
    this(location.latitude, location.longitude, DEFAULT_PRECISION);
}
 
Example #26
Source File: GeoHashQuery.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
public static Set<GeoHashQuery> queriesAtLocation(GeoLocation location, double radius) {
    int queryBits = Math.max(1, Utils.bitsForBoundingBox(location, radius));
    int geoHashPrecision = (int) Math.ceil((float)queryBits /Base32Utils.BITS_PER_BASE32_CHAR);

    double latitude = location.latitude;
    double longitude = location.longitude;
    double latitudeDegrees = radius/Constants.METERS_PER_DEGREE_LATITUDE;
    double latitudeNorth = Math.min(90, latitude + latitudeDegrees);
    double latitudeSouth = Math.max(-90, latitude - latitudeDegrees);
    double longitudeDeltaNorth = GeoUtils.distanceToLongitudeDegrees(radius, latitudeNorth);
    double longitudeDeltaSouth = GeoUtils.distanceToLongitudeDegrees(radius, latitudeSouth);
    double longitudeDelta = Math.max(longitudeDeltaNorth, longitudeDeltaSouth);

    Set<GeoHashQuery> queries = new HashSet<>();

    GeoHash geoHash = new GeoHash(latitude, longitude, geoHashPrecision);
    GeoHash geoHashW = new GeoHash(latitude, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashE = new GeoHash(latitude, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    GeoHash geoHashN = new GeoHash(latitudeNorth, longitude, geoHashPrecision);
    GeoHash geoHashNW = new GeoHash(latitudeNorth, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashNE = new GeoHash(latitudeNorth, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    GeoHash geoHashS = new GeoHash(latitudeSouth, longitude, geoHashPrecision);
    GeoHash geoHashSW = new GeoHash(latitudeSouth, GeoUtils.wrapLongitude(longitude - longitudeDelta), geoHashPrecision);
    GeoHash geoHashSE = new GeoHash(latitudeSouth, GeoUtils.wrapLongitude(longitude + longitudeDelta), geoHashPrecision);

    queries.add(queryForGeoHash(geoHash, queryBits));
    queries.add(queryForGeoHash(geoHashE, queryBits));
    queries.add(queryForGeoHash(geoHashW, queryBits));
    queries.add(queryForGeoHash(geoHashN, queryBits));
    queries.add(queryForGeoHash(geoHashNE, queryBits));
    queries.add(queryForGeoHash(geoHashNW, queryBits));
    queries.add(queryForGeoHash(geoHashS, queryBits));
    queries.add(queryForGeoHash(geoHashSE, queryBits));
    queries.add(queryForGeoHash(geoHashSW, queryBits));

    // Join queries
    boolean didJoin;
    do {
        GeoHashQuery query1 = null;
        GeoHashQuery query2 = null;
        for (GeoHashQuery query: queries) {
            for (GeoHashQuery other: queries) {
                if (query != other && query.canJoinWith(other)) {
                    query1 = query;
                    query2 = other;
                    break;
                }
            }
        }
        if (query1 != null && query2 != null) {
            queries.remove(query1);
            queries.remove(query2);
            queries.add(query1.joinWith(query2));
            didJoin = true;
        } else {
            didJoin = false;
        }
    } while (didJoin);

    return queries;
}
 
Example #27
Source File: GeoUtils.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
public static double distance(GeoLocation location1, GeoLocation location2) {
    return distance(location1.latitude, location1.longitude, location2.latitude, location2.longitude);
}
 
Example #28
Source File: GeoQueryEventTestListener.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onKeyMoved(String key, GeoLocation location) {
    if (recordMoved) {
        addEvent(keyMoved(key, location.latitude, location.longitude));
    }
}
 
Example #29
Source File: GeoQueryEventTestListener.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onKeyEntered(String key, GeoLocation location) {
    if (recordEntered) {
        addEvent(keyEntered(key, location.latitude, location.longitude));
    }
}
 
Example #30
Source File: GeoQueryDataEventTestListener.java    From geofire-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
  if (recordChanged) {
    addEvent(dataChanged(dataSnapshot.getKey(), location.latitude, location.longitude));
  }
}