Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 780
TiagoMartins Avatar asked Feb 24 '26 11:02

TiagoMartins


1 Answers

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.

like image 154
Tijmen Avatar answered Feb 26 '26 00:02

Tijmen