netcore使用mongodb实现地理围栏--多边形

(一)先使用mongodb查询

1.使用百度地图坐标提取工具,获取经纬度http://api.map.baidu.com/lbsapi/getpoint/

地理围栏坐标 

智恒:113.914586,22.556025
 街心公园:113.935355,22.549283
 创新大厦:113.932264,22.555157
 爱普生大厦:113.941751,22.560898
 君翔达大厦:113.926803,22.566505
 深圳艺术学校:113.931402,22.560497

测试坐标

中山公园--之内:
113.9263,22.553622
博伦学校--之内:
113.932085,22.562667

沁园--之外:
113.916598,22.560314
海滨中学-之外
113.905315,22.563651


2.进入mongodb库后,创建索引

db.geo.ensureIndex(  
    {  
        test1: "2dsphere"  
    }  
);

3.插入地理围栏图形:第一个坐标与最后一个坐标要一致,以保持图形闭合。

db.geo.insert(
    {
        test1:
        {
            type:"Polygon",
            coordinates:[[
                [113.914586,22.556025],
                
                [113.935355,22.549283],
                [113.932264,22.555157],
                [113.941751,22.560898],
                [113.926803,22.566505],
                [113.931402,22.560497],
                
                [113.914586,22.556025]
            ]]
        }
    }
);

4.坐标测试

中山公园--之内:如果在范围之内,会有返回数据提示

db.geo.find(
    {
        test1:
        {
            $geoIntersects:
            {
                $geometry:{ 
                    "type" : "Point",
                    "coordinates" : [113.9263,22.553622] }
                }
            }
        }
);

博伦学校--之内:如果在范围之内,会有返回数据提示

db.geo.find(
    {
        test1:
        {
            $geoIntersects:
            {
                $geometry:{ 
                    "type" : "Point",
                    "coordinates" : [113.932085,22.562667] }
                }
            }
        }
);

沁园--之外:如果在范围之内,无返回数据

db.geo.find(
    {
        test1:
        {
            $geoIntersects:
            {
                $geometry:{ 
                    "type" : "Point",
                    "coordinates" : [113.916598,22.560314] }
                }
            }
        }
);

海滨中学-之外:如果在范围之内,无返回数据

db.geo.find(
    {
        test1:
        {
            $geoIntersects:
            {
                $geometry:{ 
                    "type" : "Point",
                    "coordinates" : [113.905315,22.563651] }
                }
            }
        }
);

(二)使用netcore对外提供接口

原文地址:https://www.cnblogs.com/tangyan/p/13679881.html