Java Code Examples for com.firebase.geofire.GeoLocation#coordinatesValid()

The following examples show how to use com.firebase.geofire.GeoLocation#coordinatesValid() . 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: 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 2
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);
}