【发布时间】:2016-06-05 10:14:16
【问题描述】:
我正在实现一个应用程序,它应该显示在 Google 地图 Points、LineStrings 和 Polygons 上。
我正在使用 Mongoose 架构,它允许存储这 3 种数据,并且我可以毫无问题地发布所有这些数据。
我已经能够绘制Points,因为他们每个lat, lng 对只有1 个条目,但是,我真的很难理解如何从LineString 和Polygon lat, lng db,将它们存储在矩阵中,然后将这些对象绘制到地图中。
这就是我想要为 LineStrings 做的事情:
var valueToPush = [];
// Loop through all of the JSON entries provided in the response
for (var i = 0; i < response.length; i++) {
var user = response[i];
if (user.location.type === "LineString") {
for (var j = 0; j < user.location.coordinates.length; j++) {
valueToPush[j] = user.location.coordinates[j];
valueToPush[j+1] = user.location.coordinates[j+1];
}
}
return valueToPush;
};
console.log(valueToPush); //Printing the right LineString Points
这就是我尝试绘制 LineStrings 的方式:
var initialize = function() {
valueToPush.forEach(function(){
var myPath = new google.maps.Polyline({
path: parseFloat(valueToPush),
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
myPath.setMap(map);
});
}
但从后者我得到InvalidValueError: not an Array js?key=myKey
这是我的 Mongoose 架构:
// Pulls Mongoose dependency for creating schemas
var mongoose = require('mongoose');
var GeoJSON = require('geojson');
var Schema = mongoose.Schema;
var LocationSchema = new Schema({
name: {type: String, required: true},
location: {
type: {type : String, required: true},
coordinates : [Schema.Types.Mixed]
},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
LocationSchema.pre('save', function(next){
now = new Date();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now
}
next();
});
// Indexes this schema in 2dsphere format (critical for running proximity searches)
LocationSchema.index({location: '2dsphere'});
module.exports = mongoose.model('mean-locations', LocationSchema);
这就是我使用 Postman 发布新 LineString 的方式:
{
"name": "FirstPolyline",
"location": {
"type":"LineString",
"coordinates":
[ [100.0, 0.0], [101.0, 0.0] ]
}
}
我做错了什么?我该如何解决这个问题?
提前致谢。
【问题讨论】:
标签: javascript node.js mongodb google-maps geojson