Skip to content Skip to sidebar Skip to footer

How Do I Stringify A Google LatLng Object?

I'm working with the google maps API and trying to stringify an array of points, which each have a latitude and longitude. What I want to do is stringify the data and store it in a

Solution 1:

JSON.stringify isn't acting weird... Google maps API sends out their JSON with random keys and you can't work with their keys unfortunately...

If you need to get the latitude and longitude you need to use their API for the same...

You need to use the methods:

lat() and lng() //refer: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

And since you are dealing with points here you need to use the Points API and in this case the lat/lng are properties...

Refer to:http://code.google.com/apis/maps/documentation/javascript/reference.html#Point

If you still wanna go ahead and use stringify :

JSON.stringify({lat: testPoint.y , lon: testPoint.x);

Solution 2:

Simply use JSON.stringify({lat: testPoint.lat(), lon: testPoint.lon()}).


Solution 3:

Method below will do what you want

var ar = [{"Ia":35.99663,"Ja":-78.94982},{"Ia":35.996370000000006,"Ja":-78.94914},{"Ia":35.99595,"Ja":-78.94833000000001}];

var newAr = [];
for(var i = 0; i < ar.length; i++)
{
   var obj = new Object();
   var u = 0;
   for(var x in ar[i])
   { 

        if(u == 0)
         obj['lat'] = ar[i][x];
        else
         obj['lon'] = ar[i][x];
      u++;
    }
    newAr.push(obj);
}
alert(newAr.toSource());

What I have done: 1. I have created new array 2. Then I have looped through ar 3. Then I have looped through object properties from ar 4. and I have assigned values of properties to new properties

and that is it


Post a Comment for "How Do I Stringify A Google LatLng Object?"