I have function to get a current address(that works well) which is triggered by another jquery function that i use to pass results with ajax to mysql.
script:
var currentlocation;
function getlocation(){
navigator.geolocation.getCurrentPosition(
function( position ){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var google_map_pos = new google.maps.LatLng( lat, lng );
var google_maps_geocoder = new google.maps.Geocoder();
google_maps_geocoder.geocode(
{ 'latLng': google_map_pos },
function( results, status ) {
if ( status == google.maps.GeocoderStatus.OK && results[0] ) {
console.log( results[0].formatted_address );
currentlocation = results[0].formatted_address;
console.log('cl1: ' + currentlocation);
}
}
);
},
function(){
}
);
}
//and jquery function (there are multiple functions similar to this one using the getlocation()
$(document).ready(function () {
$('[id^=start-]').on('click', function (e) {
getlocation();
console.log('cl2: ' + currentlocation);
var locationsql = currentlocation;
console.log('cl3:' + locationsql);
});
});
console prints out undefined results for cl2 and cl3, then after a while correct result of google location in cl1.
So what I have a problem with is getting the variable currentlocation so I can use it later as in this case locationsql
Advice highly apriciated