【问题标题】:Draw Polylines and Polygons based on MongoDB stored GeoJson Coordinates基于MongoDB存储的GeoJson坐标绘制折线和多边形
【发布时间】:2016-06-05 10:14:16
【问题描述】:

我正在实现一个应用程序,它应该显示在 Google 地图 PointsLineStringsPolygons 上。

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

我已经能够绘制Points,因为他们每个lat, lng 对只有1 个条目,但是,我真的很难理解如何从LineStringPolygon 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


    【解决方案1】:

    尝试创建一个循环遍历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&lt;LatLng&gt;|Array&lt;LatLng|LatLngLiteral&gt;。检查docs

    【讨论】:

    • 我一回家就试试。谢谢。
    • 调用new_polyline.setMap(map); 会得到InvalidValueError: setMap: not an instance of Map。有什么想法吗?
    • 您在哪里以及如何初始化您的 map ?你能分享那部分代码吗?您需要将引用map 对象的变量作为第二个参数传递给addPolygonaddPolyline 函数。
    • 您的代码非常混乱,从您分享的部分看不清楚,您以什么顺序以及如何调用convertToMapPointsinitialize 函数。但要完成这项工作,您需要在 convertToMapPoints 之前调用 initialize 函数并生成变量 var map; 全局(将其从 initialize 函数中取出)或以某种方式将其传递给 convertToMapPoints 函数。
    猜你喜欢
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2015-02-27
    相关资源
    最近更新 更多