Behrouz Hosseini K.
Behrouz Hosseini K.

Reputation: 135

Issue on Converting Array of Coordinates to JSON

I am trying to parse stored items of an Array which contains Coordinates of Drawing shapes on the map as into JSON Object/String:

var polys =[];

google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
  coordinates = (polygon.getPath().getArray());
  polys.push(coordinates);
});

I used this loop to convert the array to JSON data:

var info = [];
for(var i = 0; i < polys.length; i++){
    info.push({
        "type":"POL",
        "id": i,
        "geometry": polys[i]
    });
}

every thing fine but at result I am getting a "d" and "e" keys for the coordinates as:

[
   {
      "type":"POL",
      "id":0,
      "geometry":[
         {
            "d":49.26870064827097,
            "e":-122.89237976074219
         },
         {
            "d":49.25436113302859,
            "e":-122.9092025756836
         },
         {
            "d":49.24965507167121,
            "e":-122.88551330566406
         }
      ]
   },

Can you please let me know why this is happening? Since I am going to load the JSON data into MYSQL database, do you think this is a good approch to continue?

Upvotes: 0

Views: 714

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117334

The objects with the e-and d-properties are google.maps.LatLng's, you must translate them into an array:

var info = [];
for(var i = 0; i < polys.length; i++){
    geometry=[];
    for(var j=0;j<polys[i].length;++j){
      geometry.push([polys[i][j].lat(),polys[i][j].lng()]);
    }
    info.push({
        "type":"POL",
        "id": i,
        "geometry": geometry
    });
}

To get the encoded path use this:

var info = [];
for(var i = 0; i < polys.length; i++){

    info.push({
        "type":"POL",
        "id": i,
        "geometry": google.maps.geometry.encoding.encodePath(polys[i])
    });
}

Note: you must load the geometry-library when you want to use the encoding, this library is not loaded by default

Upvotes: 1

Related Questions