Asp. NetCoreMongoDB Spatial Location (Point) and Distance Retrieval

https://lbs.amap


https://lbs.amap.com/tools/picker

2

 119.647437,29.083693 119.639798,29.080843 119.644604,29.074617 119.652758,29.075029 119.642716,29.072554 119.636579,29.072366 119.644433,29.079568 119.655548,29.08088 119.658809,29.073379 119.639497,29.06824 119.63645,29.081855 119.635592,29.078292 119.649325,29.082606 119.666963,29.082456 119.666491,29.073604 119.658509,29.070716


mongodb

db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.647437,29.083693]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.639798,29.080843]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.644604,29.074617]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.652758,29.075029]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.642716,29.072554]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.636579,29.072366]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.644433,29.079568]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.655548,29.08088]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.658809,29.073379]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.639497,29.06824]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.63645,29.081855]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.635592,29.078292]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.649325,29.082606]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.666963,29.082456]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.666491,29.073604]}})db.mapinfo.insert({"address": "", "name":"", "location": {"type": "Point", "coordinates": [119.658509,29.070716]}})

mongodb 2d 2dsphere

2d 2dsphere mongodb

2dsphere 2d mongodb2.6

db.mapinfo.ensureIndex({location : "2dsphere"})


1921 119.627438,29.078988

2000

db.mapinfo.find({location: {$near: {$geometry: {type: "Point", coordinates: [119.627438,29.078988]}, $maxDistance: 2000}}})

db.mapinfo.find({location: {$nearSphere: {$geometry: {type: "Point", coordinates: [119.627438,29.078988]}, $maxDistance: 2000}}})



$nearSphere aggregate

aggregate

1.

2.

db.mapinfo.aggregate([   {     $geoNear: {        near: {type: "Point", coordinates: [119.627438,29.078988]},        distanceField: "distance",        spherical: true,               maxDistance: 15000,				query: {			    address: {$regex:''}				}     }   },   { $skip: 0 },   { $limit: 2 }])


aggregate


16378137, 0.0016378

db.mapinfo.find({    "location": {        "$geoWithin": {            "$centerSphere": [                [                    119.627438,29.078988                ],                0.025 //             ]        }    }})



MongoDB

.net

mapinfo

public class mapinfo    {        /// <summary>        ///         /// </summary>        [BsonId]        [BsonRepresentation(BsonType.ObjectId)]        public string id { get; set; }        /// <summary>        ///         /// </summary>        public string address { get; set; }        /// <summary>        ///         /// </summary>        public string name { get; set; }        /// <summary>        ///         /// </summary>        public double? distance { get; set; }        /// <summary>        ///         /// </summary>        public GeoJsonPoint<GeoJson2DGeographicCoordinates> location { get; set; }    }

IMapinfoMongoRepository

 public interface IMapinfoMongoRepository    {        public List<Models.MongoEnity.mapinfo> QueryNear(string keywords,int pageIndex, int pageSize, double longitude, double latitude, int maxDistance);    }

MapinfoMongoRepository

public class MapinfoMongoRepository : IMapinfoMongoRepository    {        private readonly ILogger<MapinfoMongoRepository> _logger;        private readonly IMongoDbGenericHelper<Models.MongoEnity.mapinfo> _mapinfoHelper;        public MapinfoMongoRepository(ILogger<MapinfoMongoRepository> logger, IMongoDbGenericHelper<Models.MongoEnity.mapinfo> mapinfoHelper)        {            _logger = logger;            _mapinfoHelper = mapinfoHelper;        }        /// <summary>        ///         /// </summary>        /// <param name="keywords"></param>        /// <param name="pageIndex"></param>        /// <param name="pageSize"></param>        /// <param name="longitude"></param>        /// <param name="latitude"></param>        /// <param name="maxDistance"></param>        /// <returns></returns>        public List<Models.MongoEnity.mapinfo> QueryNear(string keywords, int pageIndex, int pageSize, double longitude, double latitude, int maxDistance)        {               IList<IPipelineStageDefinition> stages = new List<IPipelineStageDefinition>();            string temp1 = "";            if (!string.IsNullOrEmpty(keywords))            {                temp1 = "{"$geoNear": {"near": {"type": "Point","coordinates": [" + longitude + "," + latitude + "]},"distanceField": "distance","spherical": true,"maxDistance": " + maxDistance + ",query:{"address": { $regex: '" + keywords + "' }}}}";            }            else            {                temp1 = "{"$geoNear": {"near": {"type": "Point","coordinates": [" + longitude + "," + latitude + "]},"distanceField": "distance","spherical": true,"maxDistance": " + maxDistance + "}}";            }            PipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo> stage1 = new JsonPipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo>(temp1);            stages.Add(stage1);            string temp2 = "{$skip:" + (pageIndex - 1) * pageSize + "}";            PipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo> stage2 = new JsonPipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo>(temp2);            stages.Add(stage2);            string temp3 = "{$limit:" + pageSize + "}";            PipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo> stage3 = new JsonPipelineStageDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo>(temp3);            stages.Add(stage3);            PipelineDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo> pipeline = new PipelineStagePipelineDefinition<Models.MongoEnity.mapinfo, Models.MongoEnity.mapinfo>(stages);            //            List<Models.MongoEnity.mapinfo> result = _mapinfoHelper.GetCollection(nameof(Models.MongoEnity.mapinfo)).Aggregate(pipeline).ToList();            return result;        }    }

Action

 [HttpGet]        public IActionResult TestQueryNear()        {            var data = _mapinfoMongoRepository.QueryNear(keywords: string.Empty, pageIndex: 1, pageSize: 2, longitude: 119.627438, latitude: 29.078988, maxDistance: 15000);            return Ok(data);        }


distance

[  {    "id": "64ae31fe6412e519c80f4b3d",    "address": "",    "name": "",    "distance": 797.0567583679608,    "location": {      "Coordinates": {        "Values": [          119.635592,          29.078292        ],        "Longitude": 119.635592,        "Latitude": 29.078292      },      "Type": 7,      "BoundingBox": null,      "CoordinateReferenceSystem": null,      "ExtraMembers": null    }  },  {    "id": "64ae31fe6412e519c80f4b3c",    "address": "",    "name": "",    "distance": 933.0219480837998,    "location": {      "Coordinates": {        "Values": [          119.63645,          29.081855        ],        "Longitude": 119.63645,        "Latitude": 29.081855      },      "Type": 7,      "BoundingBox": null,      "CoordinateReferenceSystem": null,      "ExtraMembers": null    }  }]

guanzhu


Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.(Email:[email protected])