GeoPoint 表示坐标点,通过 new GeoPoint(longitude, latitude) 创建一个点,其中经度(longitude)在前,纬度(latitude)在后
GeoPolygon 表示地理形状,可以通过以下两种方法创建一个地理形状
// 1. 直接使用数字
GeoPolygon polygon = new GeoPolygon(
new float[]{10f, 10f},
new float[]{20f, 20f},
new float[]{30f, 30f}
);
// 2. 借助 GeoPoint
GeoPoint p1 = new GeoPoint(10f, 10f);
GeoPoint p2 = new GeoPoint(10f, 10f);
GeoPoint p3 = new GeoPoint(10f, 10f);
GeoPolygon polygon = new GeoPolygon(p1, p2, p3);
请求示例
Table geoTest = new Table("geo_test");
Record record = geoTest.createRecord
// 保存一个点
record.put("location", new GeoPoint(10f, 20f)).save
// 保存一个多边形
record.put("location", new GeoPolygon(
new GeoPoint(10f, 20f),
new GeoPoint(10f, 20f),
new GeoPoint(10f, 20f)
)).save();
地理位置查询
include 在指定多边形集合中找出包含某一点的多边形
// 查找当前用户所属小区
Table neighbourhood = new Table("neighbourhood");
// geoField 为 neighbourhood 表中定义地理位置的字段名,point 为用户所在位置,为 GeoPoint 类型
Where where = new Where();
where.include("geoField", point);
Query query = new Query();
query.put(where);
neighbourhood.query(query);
// 查找在距离用户 radius 千米范围内的饭店
Table restaurant = new Table("restaurant");
// geoField 为 restaurant 表中定义地理位置的字段名
Where where = new Where();
where.withinCircle("geoField", point, radius);
Query query = new Query();
query.put(where);
restaurant.query(query);