0

I am developing a script to get the latlng from google maps by the address.

If I put an alert(jsonarr.lat); inside the function map_address() I get the correct value, but if I assign the result to a variable like this:

var coord = map_address('address');

alert(coord.lat);   

I get the error coord is undefined

function map_address(addr)
    {

        var input_address = addr;
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode( { address: input_address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var lat = results[0].geometry.location.lat();
                var lng = results[0].geometry.location.lng();


                      jsonarr={'lat':lat,'lng':lng}
                      alert(jsonarr.lat);
                      return jsonarr;           
            } else {
                alert("Nessuna coordinata trovata da questo indirizzo!");
            }
        });
    }
1
  • You can't return from there, it's an asynchronous callback. Commented Nov 22, 2012 at 17:10

3 Answers 3

1

Try like this:

function map_address(addr, callback) {

    var input_address = addr;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { address: input_address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat();
            var lng = results[0].geometry.location.lng();


                               jsonarr={'lat':lat,'lng':lng}
                               alert(jsonarr.lat);
                               callback(jsonarr) ;


                            }
        else {
            alert("Nessuna coordinata trovata da questo indirizzo!");
            }
        });
}
map_address("hogehoge", function(result){
  alert(result)
});
Sign up to request clarification or add additional context in comments.

Comments

1

the geocoder.geocode(..); function is asynchronous as it wraps another function inside that will be called later when the geocode operation is done.

because of this map_address(...) will always return undefined

Comments

0

Thanks all for the answers, i put all my code inside the callback and all work fine this is the final code.

   function map_address(addr,callback)
        {

            var input_address = addr;
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { address: input_address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) 
                {
                    var lat = results[0].geometry.location.lat();
                    var lng = results[0].geometry.location.lng();

                    jsonarr={'lat':lat,'lng':lng}                       
                    return callback(jsonarr);                        
                }
                else {
                    alert("No coord find");
                }
            });
        }


$(document).ready(function(){ 
   $(window).load(function(){ 

map_address('address string',function(coord){


  var center=new google.maps.LatLng(coord.lat,coord.lng);



  var settings = {
          zoom: 16,
          center: center,
          mapTypeControl: false,
          mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
          navigationControl: true,
          navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
          mapTypeId: google.maps.MapTypeId.ROADMAP
      }; 

  var map=new google.maps.Map(document.getElementById("map"), settings);

var marker = new google.maps.Marker({
position: new google.maps.LatLng(coord.lat,coord.lng),
map: map
});


});
});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.