【问题标题】:Get lat and lng using $near and $geometry not working on Mongo + meteor使用 $near 和 $geometry 获取 lat 和 lng 不适用于 Mongo + meteor
【发布时间】:2016-07-18 14:50:25
【问题描述】:

我正在尝试获取距中心点 200m 范围内的所有文档,但出现此错误

来自子 getLocNearMe id hg3jRDv8onsZEGvBM 的异常错误:异常 轮询查询时 {"collectionName":"Col_Location","selector":{"loc":{"$near":{"$geometry":{"type":"Point","coordinates":[1.3852457,103.88112509999999]}, "$maxDistance":200}}},"options":{"transform":null}}: $near 需要一个点,给定 { 类型:“点”,坐标:[ 1.3852457, 103.8811251] }

客户端/组件/map.jsx

navigator.geolocation.getCurrentPosition(function (pos) {    
    const sub = Meteor.subscribe("getLocNearMe", pos.coords.latitude, pos.coords.longitude)

    Tracker.autorun(function () {
        if (sub.ready()) {
            Data.MarkersRawData = Col_Location.find().fetch()
        }
    })
})

lib/collections.jsx

Meteor.publish("getLocNearMe", function(lng, lat) {
    check(lat, Number)
    check(lng, Number)

    const data = Col_Location.find({
        loc: {
            $near: {
                $geometry: {
                    type: "Point" ,
                    coordinates: [lng, lat]
                },
                $maxDistance: 200
            }
        }
    })

    console.log(data)
    if (data) {
        return data
    }

    return this.ready()
})

服务器/server.jsx

Col_AllQuestion._ensureIndex({"loc": "2dsphere"})

    Col_Location.insert({
        loc: {
            type: "Point",
            coordinates: [103.8, 1.31]
        }
    })

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    http://geojsonlint.com/ 测试你的“Point”会带来几个问题。似乎(有点挑剔)规范要求您的键被引用字符串(我认为流星为您处理),而且您的坐标被翻转(纬度/经度)这一事实。

    像这样将样本放入会得到有效的 GeoJSON 结果:

    { 
      "type": "Point", 
      "coordinates": [ 103.8811251, 1.3852457 ] 
    }
    

    【讨论】:

    • @phongyewtong 很高兴听到,如果它解决了您的问题,请接受答案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多