PDA

View Full Version : Pass geo coordinates to ajax post request



won.rhee
22 Jul 2011, 12:20 PM
Hello, I've been scratching my head on this and hoping to gain some understanding from your replies.



Ext.regApplication('NotesApp',{
launch: function(){
var mylat = 123;

var geo = new Ext.util.GeoLocation({
autoUpdate : false
});

geo.updateLocation(
function(){
mylat = geo.latitude; // I was expecting mylat to be re-assigned
}
);

console.log(mylat); //still points to 123

Ext.Ajax.request({
url: 'datajson.php',
method: 'post',
params:{
latitude: mylat //i want to pass geo.latitude
},
success: function(response){
console.log(response)
},
fail: function(){
console.log('Failed to get data from server')
}
})
}
});


All I want to do is pass geo.location to params.latitude property
I am just beginning to learn javascript and trying to learn. I have a feeling I'm misunderstanding scope issues. Also I noticed
"console.log(mylat); //still points to 123" gets fired before ajax request completes.

Any pointers would be greatly appreciated.

jgostylo
22 Jul 2011, 12:51 PM
I think this is the issue.

You provide the callback function:

function(){
mylat = geo.latitude; // I was expecting mylat to be re-assigned
}

But the callback function expects a parameter for the geo object so it should look more like:

function(paramGeo){
mylat = paramGeo.latitude; // I was expecting mylat to be re-assigned
}

It you are just asking for the global geo object which doesn't have any data yet.

won.rhee
22 Jul 2011, 3:04 PM
So I change the code to the following and worked out well.
Thank you for your pointer. Really appreciate that.



Ext.regApplication('NotesApp',{
launch: function(){

var geo = new Ext.util.GeoLocation({
autoUpdate : false
});

geo.updateLocation(function(geo){myajaxrequest(geo)});

function myajaxrequest (geo){

Ext.Ajax.request({
url: 'datajson.php',
method: 'post',
params:{
latitude: geo.latitude,
longitude: geo.longitude
},
success: function(response){
console.dir(response);
},
fail: function(){
console.log('Failed to get data from server')
}
});

}
});