How can I create a google LatLng object?

I am working with google API and trying to create an array of points, each of which has latitude and longitude. What I want to do is organize the data and save it in the database so that it can be found later. The problem is that because google maps store and use data, the result of JSON.stringify strangely random. 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 saw about ten different variations. This causes a lot of problems when I store gated data in a 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 like this:

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

I already wrote the code to find out which letter cards are used. With this information, I want to replace each random number of random letters with "lat" or "lon". I hope to use a replacement method this way ...

 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 cannot figure out how to replace the keys. I searched and found several similar cases on the Internet: JSON.stringify, change the key case and Replace the JSON keys in javascript . However, I could not understand them and could not find a solution that suited my needs.

Thanks in advance for any help or suggestions!


Update!

Thanks to everyone. 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); } 
+4
source share
3 answers

The 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 did: 1. I created a new array 2. Then I went in cycles through ar 3. Then I went in cycles on properties of objects from ar 4. And I assigned property values ​​to new properties

and this is he

+1
source

JSON.stringify doesn't act weird .. google maps api sends its json with random keys and you cannot work with your keys, unfortunately.

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

you need to use methods

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

and since you are dealing with points that you use to use the points API, in which case the lat / lng properties are properties.

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

if you still want to continue and use stringify:

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

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

+1
source

All Articles