Goltsev Eugene
Goltsev Eugene

Reputation: 3637

Geocoding know if location is near another locations in database

Ok, I have a database with many location records there (storing latitude & longitude).
When new potential record comes, I need to check - if it is closer than 50 meters to any of existing records - I should not include it in database.

I know there is a possibility to calculate distance between two locations.
But in order to do that I need extract all records from my database and then in loop compare potential record to every existing. I suppose it will take some time.

I would like to have some delta, on which I can increase/decrease latitude & longitude of potential record - and that delta would give me 50 meters distance radius.

For example, I'd like to have something like that:

    public boolean needsToBeIncluded(double newLat, double newLng) {
        int delta = 0;
        Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM myLocations WHERE latitude between " + (newLat - delta) + " and " + (newLat + delta) +
                " AND longitude between "  + (newLng - delta) + " and " + (newLng + delta), null); 
        return c.getCount() > 0;
    }

Is it possible or I have wrong imagination about latitude & longitude? Thx.

Upvotes: 2

Views: 539

Answers (1)

jwd630
jwd630

Reputation: 4675

You could compute the geohash value of each of your location records. Comparing the prefix portions of location records' geohash values enables determining their nearness. This GIS Stack Exchange entry helps explain geohashes while this Stack Exchange entry explains the accuracy of adjacent geohash cells - the point where two geohash sequences start to differ determines their delta.

Upvotes: 1

Related Questions