0

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Dr.Molle, just have a quick question as well, I saw your post for saving data in a database and encoding the geometery hints. can you pleae also let me know how I can encode the gemoetry in my example?
I am also getting this error Uncaught TypeError: Object [object Array] has no method 'lat'
Thanks Dr. but how about my second question? do you think this is a good approch to load into database?
Great Thaks a lot. Just one last question, if I want to read encoded json how can I do that?
added an example above. It's a good approach when you only want to store the data, because a encoded polyline takes less space. But when you want to be able to fetch data from the DB based on a geometric condition you can't use the encoded path. When you want to read the data you must decode the encoded path (also with the geometry-library, the method is google.maps.geometry.encoding.decodePath())
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.