好的,现在有这么一个需求。你所开发的应用的数据库中有全市所有饭店的经纬度坐标,现在你在某地定位了一个坐标,应用如何给你找出最近的饭店。多维空间搜索,使用RTree。这里只推荐已有的实现了RTree的jar包。
maven项目,可下载jar包,并且这是一个简单的例子:https://github.com/aled/jsi-examples
在线API:http://jsi.sourceforge.net/apidocs/index.html
下面附上自己很low的测试代码:
net.sf.jsi.examples;
gnu.trove.procedure.TIntProcedure;
net.sf.jsi.Point;
net.sf.jsi.Rectangle;
net.sf.jsi.SpatialIndex;
net.sf.jsi.rtree.RTree;
TestRTree {
run() {
Point point = Point(, );
Rectangle[] rectangle = Rectangle[];
rectangle[] = Rectangle(, , , );
rectangle[] = Rectangle(, , , );
rectangle[] = Rectangle(-, -, -, -);
rectangle[] = Rectangle(-, -, -, -);
SpatialIndex si = RTree();
si.init();
(i = ; i < rectangle.; i++) {
si.add(rectangle[i], i);
}
si.nearestN(point, TIntProcedure() {
execute(i) {
System..println(+ i + + [i] + + [i].distance());
;
}
}, , Float.);
}
main(String[] args) {
TestRTree().run();
}
}