0

我可以使用 2dsphere 索引计算 MongoDB 中从点到点的距离。

但是,现在我需要计算从给定点到多边形的距离。

我正在使用以下代码:

  Document commandNoIntersect = new Document("geoNear", "Location")
                            .append("near", new com.mongodb.client.model.geojson.Point(new Position(Longitude, Latitude)))
                            .append("spherical", true)
                            .append("query", new Document(new Document("location", locationId)))
                            .append("distanceMultiplier", 0.001);

database.runCommand(commandNoIntersect);

我的 2dsphere 索引是多边形的坐标。结果与点对点距离不一致。

感谢您的反馈,

4

1 回答 1

0

我使用 mongo 驱动程序解决了这个问题:

mongoTemplate.geoNear( NearQuery.near(new Point(new Point(pin.getDinamycPoint().getPointId().getY(), pin.getDinamycPoint().getPointId().getX()))).spherical(true)。 inKilometers().query(new Query().addCriteria(Criteria.where("locationId").is(locationId))), Location.class);

这给了我从点到 GeoJsonPolygon 中最近点的正确距离。

必须在集合的 GeoJsonPolygon 字段中创建 2dsphere 索引。

于 2018-09-26T12:55:43.600 回答