TiagoMartins
TiagoMartins

Reputation: 100

javascript arrays on gmaps

i'm newbie in javascript so, in this example exists the geometrycontrols.js (for global controls) and markercontrol.js (for marker controls)

my problem is identify the arrays where "data" is saved...

at the reference i see a savedata function but i have no idea how work with this function...

on the other side, in test.html if i've the outup on the Glog startup and output "data", and let me thinking that comes from array...

My objective is save the coordinates and other all atributes to mysql database, and when i discover where are "data" is the easy part.

if someone worked with this example (or not) can help me i'm grateful

ps: i'm really a newbie on javascript :P

edit1: I was out for a time, and now I focus in geometrycontrols.js specially in: GeometryControls.prototype.saveData = function(opts){ var me = this; if(opts.allData === true){ //me.saveAllData(); } else { //construct a json data record var geomInfo = opts.geomInfo, index = opts.geomInfo.index; var record = geomInfo.storage[index];
var recordJSON = {}; recordJSON.type = record.type; recordJSON.coordinates = [];

//determine geometry type, and copy geometry appropriately
if(record.type === "point"){
  recordJSON.coordinates.push({lat:record.geometry.getLatLng().lat(),lng:record.geometry.getLatLng().lng()});
 alert(recordJSON.coordinates);
} else {
    alert("is not point");
  var vertex;
  for(var i=0;i<record.geometry.getVertexCount();i++){
    vertex = record.geometry.getVertex(i);
    recordJSON.coordinates.push({lat:vertex.lat(),lng:vertex.lng()});

  }

}

//add title and description
recordJSON.title = record.title[0];
recordJSON.description = record.description[0];

//TODO add styles 
recordJSON.style = ""; //TODO}  //TODO Make separate prototype function?function postData(data){
//TODO 
me.debug(data);
//alert(recordJSON.coordinates);
//alert(data);
};postData(me.serialize(recordJSON));}; `

When I alert(recordJSON.coordinates), the outupt is [object Object] and i've no idea why, in theory this array contains the coordinates...

Upvotes: 3

Views: 656

Answers (2)

Tijmen
Tijmen

Reputation: 11

Here is some code I have used to send the data to MySQL. It uses a little bit of jQuery to do the ajax magic (the line starting with the dollarsign is jQuery).

function postData(data){
me.debug(data);      
var dataString = JSON.stringify(data);   
me.debug(dataString);
$.post('storage.php', { data: dataString });   
}; 
postData(recordJSON);

As you can see I've modified the way the 'recordJSON' object gets sent to the postData function a bit too: I've removed the serialise function.

Next, create a PHP file (called 'storage.php' in my case) and put this in it:

<?php
$received = json_decode($_POST['data'], true);
echo "just received " . $received['name'];
?>

You now have an array in PHP that you can do with as you please.

In the examplecode above I've modified the jQuery post function a bit, so if it doesn't work, look there.

Upvotes: 1

Dan U.
Dan U.

Reputation: 1357

The data is stored in JSON format in this file: http://gmaps-utility-library-dev.googlecode.com/svn/trunk/geometrycontrols/examples/data/testdata.js -- it's pretty much self-documenting, just follow the example to set your coordinates.

Note that if you need to find the latitude and longitude for a given address this is a good site: http://itouchmap.com/latlong.html

Upvotes: 0

Related Questions