-1
$(function() {
var lat = 41.9100711;
var lng = 12.535`enter code here`9979;  
var geocoder = new google.maps.Geocoder();
var address = "<?php echo $citta ?>";
geocoder.geocode( { 'address': address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK)
   {
       // do something with the geocoded result
       //
       lat = results[0].geometry.location.lat();
       lng = results[0].geometry.location.lng();

   }
});
alert("latitudine"+lat);

I need lat and lng for this plugin and i need to declare lat and lng inside this, for this is why i'm asking about to have a globa variablse to use .... in case any other idea to find lat and lng from an address to use with this plugin ?

$('#map-container').storeLocator({'deafultlat'=lat,'defaultlng'=lng});

  • 2
    You need to write a description as well as making sure that your question is formatted correctly. You're code should be in a code block to start with. – hudsond7 Sep 24 '15 at 12:39
  • Put your $("#map-container") call inside the callback of geocoder.geocode: if (status == google.maps.GeocoderStatus.OK) { // lat/lng definition $("#map-container")... } – posixpascal Sep 24 '15 at 13:25
  • In this way don't work – Giuseppe Zagaria Sep 24 '15 at 13:31
  • I can't invite you to a chat where we can talk about this error because you'll need more reputation (20 atleast, you have 17atm). Can I contact you somehow? Maybe through twitter or other? Don't post any e-mail address or different private data. Otherwise, just wait till you reach 20 reputation (maybe answer a question on stackoverflow) and we can start a chat about this problem. – posixpascal Sep 24 '15 at 17:08
  • twitter: zelltitanium http://www.giuseppezagaria.com/contatti/ – Giuseppe Zagaria Sep 25 '15 at 12:35

1 Answers1

1

The variables are changed but your alert code is outside your jQuery function:

$(function(){ /* your code */ })  
alert(lat + "/" + lng);

Since you are using var inside a function, they aren't globals anymore. What you want to do is, remove the var statements in the $() function and instead write it like this:

var lat, lng;
$(function(){
    // your current code.

})
// no alert here since lat/lng are empty when alert is called.

Be aware of JavaScript concurrency, your alert code will most likely execute before the $() method. That's because $() is a shorthand for $("document").ready() which will delay the call until, well, the document is ready.

Try to put your alert into the call back of geocoder to solve this.

Right below where you set lat/lng:

$(function() { 
    var lat = 41.9100711;
    var lng = 12.5359979;
    var geocoder = new google.maps.Geocoder(); 
    var address = ""; 
    geocoder.geocode({
        'address': address
    }, function(results, status) { 
         if (status == google.maps.GeocoderStatus.OK) { 
             // do something with the geocoded result // 
             lat = results[0].geometry.location.lat(); 
             lng = results[0].geometry.location.lng();
             alert("latitudine"+lat); // works!
         }
     }); 
});

To make lat/lng real globals you could write something like this:

window.lat = 0.0;
window.lng = 0.0;

// Now whenever you change window.lat, it's applied to all functions since window is already a global.

Or just use var outside any function to make globals.

Imo it's bad practice to use globals, you should use instead a callback whenever you need these lat/lngs.

posixpascal
  • 3,031
  • 3
  • 25
  • 44