gpt4 book ai didi

javascript - 根据 MongoDB 存储的 GeoJson 坐标绘制折线和多边形

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:09 24 4
gpt4 key购买 nike

我正在实现一个应该在 Google map 上显示PointsLineStringsPolygons 的应用程序。

我使用的是 Mongoose 架构,它允许存储这 3 种数据,并且我可以毫无问题地发布所有数据。

我已经能够绘制,因为它们每对lat,lng只有1个条目,但是,我真的很难理解如何从数据库获取LineStringPolygonlat,lng对,将它们存储在矩阵中,然后将这些对象绘制到 map 中。

这就是我正在尝试为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] ]

}
}

我做错了什么?我该如何解决这个问题?

提前致谢。

最佳答案

尝试创建一个循环来迭代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 .

关于javascript - 根据 MongoDB 存储的 GeoJson 坐标绘制折线和多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com