根据 MongoDB 存储的 GeoJson 坐标绘制多段线和多边形
Draw Polylines and Polygons based on MongoDB stored GeoJson Coordinates
我正在实现一个应该显示在 Google 地图 Points
、LineStrings
和 Polygons
上的应用程序。
我正在使用 Mongoose
模式,它允许存储这 3 种数据,我能够 post 所有这些都没有问题。
我已经能够绘制 Points
因为他们每对 lat, lng
只有 1 个条目,但是,我真的很难理解如何获取 LineString
和 Polygon
lat, lng
来自数据库的对,将它们存储在矩阵中,然后将这些对象绘制到地图中。
这就是我要为 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
这是我的 猫鼬模式:
// 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);
这就是我 post 使用 Postman 的新 LineString 的方式:
{
"name": "FirstPolyline",
"location": {
"type":"LineString",
"coordinates":
[ [100.0, 0.0], [101.0, 0.0] ]
}
}
我做错了什么?我该如何解决这个问题?
提前致谢。
尝试创建一个循环遍历 response
数组中的所有项目。然后根据项目的位置类型,调用适当的函数:
for (var i = 0; i < response.length; i++) {
var item = response[i];
if (item.location.type === "LineString") {
addPolyline(item.location.coordinates, map);
}else if(item.location.type === "Polygon") {
addPolygon(item.location.coordinates, map);
}
}
添加 lineString 的函数如下所示。使用 google.maps.Polyline
对象。
function addPolyline(coords, map){
var polyline_coordinates = [];
for(var i = 0; i < coords.length; i++){
polyline_coordinates.push({lat: coords[i][0], lng: coords[i][1]});
}
var new_polyline = new google.maps.Polyline({
path: polyline_coordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
new_polyline.setMap(map);
}
同样适用于使用 google.maps.Polygon
对象的多边形。
function addPolygon(coords, map){
var polygon_coordinates = [];
for(var i = 0; i < coords.length; i++){
polygon_coordinates.push({lat: parseFloat(coords[i][0]), lng: parseFloat(coords[i][1])});
}
var new_polygon = new google.maps.Polygon({
path: polygon_coordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.5
});
new_polygon.setMap(map);
}
在这两种情况下,您都需要创建一个路径数组。路径数组是像path:MVCArray<LatLng>|Array<LatLng|LatLngLiteral>
这样的对象数组。检查 docs.
我正在实现一个应该显示在 Google 地图 Points
、LineStrings
和 Polygons
上的应用程序。
我正在使用 Mongoose
模式,它允许存储这 3 种数据,我能够 post 所有这些都没有问题。
我已经能够绘制 Points
因为他们每对 lat, lng
只有 1 个条目,但是,我真的很难理解如何获取 LineString
和 Polygon
lat, lng
来自数据库的对,将它们存储在矩阵中,然后将这些对象绘制到地图中。
这就是我要为 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
这是我的 猫鼬模式:
// 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);
这就是我 post 使用 Postman 的新 LineString 的方式:
{
"name": "FirstPolyline",
"location": {
"type":"LineString",
"coordinates":
[ [100.0, 0.0], [101.0, 0.0] ]
}
}
我做错了什么?我该如何解决这个问题?
提前致谢。
尝试创建一个循环遍历 response
数组中的所有项目。然后根据项目的位置类型,调用适当的函数:
for (var i = 0; i < response.length; i++) {
var item = response[i];
if (item.location.type === "LineString") {
addPolyline(item.location.coordinates, map);
}else if(item.location.type === "Polygon") {
addPolygon(item.location.coordinates, map);
}
}
添加 lineString 的函数如下所示。使用 google.maps.Polyline
对象。
function addPolyline(coords, map){
var polyline_coordinates = [];
for(var i = 0; i < coords.length; i++){
polyline_coordinates.push({lat: coords[i][0], lng: coords[i][1]});
}
var new_polyline = new google.maps.Polyline({
path: polyline_coordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
new_polyline.setMap(map);
}
同样适用于使用 google.maps.Polygon
对象的多边形。
function addPolygon(coords, map){
var polygon_coordinates = [];
for(var i = 0; i < coords.length; i++){
polygon_coordinates.push({lat: parseFloat(coords[i][0]), lng: parseFloat(coords[i][1])});
}
var new_polygon = new google.maps.Polygon({
path: polygon_coordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.5
});
new_polygon.setMap(map);
}
在这两种情况下,您都需要创建一个路径数组。路径数组是像path:MVCArray<LatLng>|Array<LatLng|LatLngLiteral>
这样的对象数组。检查 docs.