搜索n范围内的所有zipcodes

时间:2016-06-28 18:11:00

标签: java mongodb geolocation geo

我正在尝试从我查询的美国任何邮政编码中查找25英里范围内的所有邮政编码。我有每个邮政编码的经纬度。我在纬度和经度上增加了25英里,以创建一个25英里的半径,但我正在努力在该范围内搜索。

    public List<HashMap<String, Object>> query(String zip, String lat, String lon) {
    final List<HashMap<String, Object>> response = new ArrayList<HashMap<String, Object>>();
    MongoClient mongoClient = null;

    try {
        mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase db = mongoClient.getDatabase("geoprofile");


        MongoCollection<Document> mongoCollection = db.getCollection("education");
        FindIterable<Document> iterable = mongoCollection.find(new Document("ZIP", Integer.valueOf(zip)));

        //NEW
        FindIterable<Document> north = mongoCollection.find(new Document("LATITUDE", Integer.valueOf(lat)+ .3623));
        FindIterable<Document> south = mongoCollection.find(new Document("LATITUDE", Integer.valueOf(lat)- .3623));
        FindIterable<Document> west = mongoCollection.find(new Document("LONGITUD", Integer.valueOf(lon) + .4579));
        FindIterable<Document> east = mongoCollection.find(new Document("LONGITUD", Integer.valueOf(lon) - .4579));


        HashMap<String, Object> currentDirection = new HashMap<String, Object>();
            currentDirection.put(north, new_north);
            currentDirection.put(south, new_south);
            currentDirection.put(west, new_west);
            currentDirection.put(east, new_east);

        result.put("new_north", currentDirection);
        result.put("new_south", currentDirection);
        result.put("new_west", currentDirection);
        result.put("new_east", currentDirection);

        //NEW

        for (Document document : iterable) {
            if (Document iterable : result.values()){
            HashMap<String, Object> data = new HashMap<String, Object>();
            data.putAll(document);  
            response.add(data);

        }

        }
        } catch (Exception ex) {
        ex.printStackTrace();

    } finally {
        mongoClient.close();
    }

    return response;

我尝试过使用API​​,但无济于事。如何在25英里范围内搜索我用于搜索的邮政编码的唯一纬度和长度?我应该考虑做其他事情还是附加我的代码?任何帮助都会很棒。谢谢!

0 个答案:

没有答案