The Geolocation API is asynchronous, so you have to wait for the data to be returned
function getLocation(callback) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
callback({
success : true,
result : position.coords.latitude + "," + position.coords.longitude
});
});
} else {
callback({
success : false,
result : "Geolocation is not supported by this browser."
});
}
}
function doRequest() {
var terms = 'turkish';
var near = 'San+Francisco';
getLocation(function(result) {
if (result.success) {
var y = result.result;
// do something with y here
}else{
x.innerHTML = result.result;
}
});
}