Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 database so that it can be retrieved later. The problem is that, because of the way google maps stores and uses data, the result of JSON.stringify is strangely random at times. For example, sometimes the data looks like this:

[{"Na":35.99663,"Oa":-78.94982},
 {"Na":35.996370000000006,"Oa":-78.94914},
 {"Na":35.99595,"Oa":-78.94833000000001},...]

And sometimes the exact same data looks like this:

[{"Ia":35.99663,"Ja":-78.94982},
 {"Ia":35.996370000000006,"Ja":-78.94914},
 {"Ia":35.99595,"Ja":-78.94833000000001},...]

In fact I've seen about ten different variations. This causes a lot of problems when I store the stringified data in the database and retrieve it, because I never know which format to expect. What I want to do is replace the keys so that the data looks more like this:

[{"lat":35.99663,"lon":-78.94982},
 {"lat":35.996370000000006,"lon":-78.94914},
 {"lat":35.99595,"lon":-78.94833000000001},...]

I've already written code to figure out which random letters google maps is using. With that information, I want to replace each occurrence of those random letters with "lat" or "lon". I hope to use a replacer method like so...

function formatData(data) {
    //data is an object that I want to stringify

    //this testPoint and testString help us figure out which letters google is using this time around
    var testPoint = new google.maps.LatLng(23,45);
    var testString = JSON.stringify(testPoint);
    var strangeLat = testString.substring(2,4); //this is a random two character string which represents latitude in the stringified data
    var strangeLon = testString.substring(10,12); //this is a random two character string which represents longitude in the stringified data

    //stringify the data and replace all occurences of the random letters with lat and lon
    var formattedData = JSON.stringify(data, function(key,value) {
        //do something here to replace the keys
    });

    return formattedData;     
}

I know how to replace values with the replacer function, but I can't figure out how to replace keys. I searched around and found a few similar cases online: JSON.stringify, change the case of the key and Replace keys JSON in javascript . However, I had trouble understanding them and was unable to come up with a solution to fit my needs.

Thanks in advance for any help or suggestions!


Update!

Thanks all. I was able to come up with a solution using a combination of your suggestions. Here's what it looks like:

function formatData(data) {
    //data is an array of many LatLng objects

    newData = [];

    for(var i = 0; i < data.length; i++) {
        var obj = new Object();
        obj['lat'] = data[i].lat();
        obj['lon'] = data[i].lng();
        newData.push(obj);
    }

    return JSON.stringify(newData);     
}
like image 208
stephenalexbrowne Avatar asked Dec 22 '22 09:12

stephenalexbrowne


1 Answers

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);
like image 92
Baz1nga Avatar answered Dec 23 '22 23:12

Baz1nga