AndreaM16 AndreaM16 - 1 year ago 335
Node.js Question

Draw Polylines and Polygons based on MongoDB stored GeoJson Coordinates

I'm realizing an application which is supposed to show on a Google Map


I'm using a
schema which allows those 3 kind of data to get stored and I'm able to post all of them with no problems.

I'm already able to draw
since they have only 1 entry per
lat, lng
pair, but, I'm really struggling with understanding how to fetch
lat, lng
couples from the db, store them in a matrix and then drawing these object into the map.

This is what I'm trying to do for 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

And this is how I'm trying to draw LineStrings:

var initialize = function() {

var myPath = new google.maps.Polyline({
path: parseFloat(valueToPush),
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2


But from the latter i get
InvalidValueError: not an Array js?key=myKey

This is my Mongoose Schema:

// 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:},
updated_at: {type: Date, default:}

// 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

// Indexes this schema in 2dsphere format (critical for running proximity searches)
LocationSchema.index({location: '2dsphere'});

module.exports = mongoose.model('mean-locations', LocationSchema);

And this is how I post a new LineString using Postman:

"name": "FirstPolyline",
"location": {
[ [100.0, 0.0], [101.0, 0.0] ]


What Am I doing wrong? How Can I fix this?

Thanks in Advance.

Answer Source

Try to create a cycle that iterates over all the items in the response array. Then depending on the item's location type, call an appropriate function:

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);

function for adding lineString would look like this. Use google.maps.Polyline object.

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

Similarly for polygons using google.maps.Polygon object.

function addPolygon(coords, map){
    var polygon_coordinates = [];
    for(var i = 0; i < coords.length; i++){
        polygon_coordinates.push({lat: coords[i][0], lng: 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

In both cases you need to create a path array. Path array is an array of objects like path:MVCArray<LatLng>|Array<LatLng|LatLngLiteral>. Check docs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download