将GeoJSON对象存储在字段“loc”

时间:2015-09-28 23:01:56

标签: javascript mongodb meteor

我在Meteor应用程序中插入一个对象listingItemInjectionObj,其中一个属性如下

listingItemInjectionObj.loc = { 
    type: "Point", 
    coordinates: [ lat, lng ] // google api lat(), lng() stored in vars
};

然而,当我插入Listings.insert(listingItemInjectionObj);时,我收到以下错误"insert failed: MongoError: insertDocument :: caused by :: 16804 location object expected, location array not in correct format"我将基于GeoJSON Objects,我在尝试this SO Q后来到这里。

2 个答案:

答案 0 :(得分:0)

您的阵列中的纬度和经度排序错误。 GeoJSON在坐标之前需要很长时间:

listingItemInjectionObj.loc = { 
    type: "Point", 
    coordinates: [ lng, lat ]
};

答案 1 :(得分:0)

Following code works:-

Request payload:
{"loc":["-12.62","21.48"]}

Node.js backend:
var lat, lon, loc = req.body.loc;
if(loc && loc instanceof Array && loc.length) {
    lat = parseInt(loc[0], 10) || 0;
    lon = parseInt(loc[1], 10) || 0;
    data['loc'] = {
        type: "Point",
        coordinates : [lat, lon]
    };
}