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(开发者_StackOverflow中文版);
obj['lon'] = data[i].lng();
newData.push(obj);
}
return JSON.stringify(newData);
}
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);
Simply use JSON.stringify({lat: testPoint.lat(), lon: testPoint.lon()})
.
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
精彩评论