1

I´m new to StackOverflow, so if I make a mistake, please be lenient with me.

I have a problem. When I am trying to access to a variable from another function, it results "null".

function initAutocomplete() {
    var latitude = 0;
    var longitude = 0;
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById("geocode").value;
    geocoder.geocode({ 'address': address }, 
    function getCoordinates(results, status) {
        //if (status == google.maps.GeocoderStatus.OK) {
             latitude = results[0].geometry.location.lat();
             longitude = results[0].geometry.location.lng();

        } 
    //}
    );
    var uluru = {lat: latitude, 
    lng: longitude};
    var map = new google.maps.Map
    (document.getElementById('map'), 
    {
      center: uluru,
      zoom: 17,
      mapTypeId: 'roadmap'
    });
}
7
  • 1
    What's the connection with php and html? Please remove those tags. Commented Jan 29, 2018 at 10:03
  • 3
    What's the question again? What line are we talking about? Commented Jan 29, 2018 at 10:04
  • 2
    Which variable is getting set to null? Commented Jan 29, 2018 at 10:04
  • @KIKOSoftware ok, I will do that. Commented Jan 29, 2018 at 11:01
  • @Keith I want to get latitude and longitude from function getCoordinates() in the var uluru Commented Jan 29, 2018 at 11:01

3 Answers 3

1

latitude and longitude for uluru are fully populated in the geocoding callback function which is not necessarily going to make them available when the function loads so instead you could use the callback to set the map center when those values are available.

function initAutocomplete() {
    var latitude = 0;
    var longitude = 0;
    var uluru = {lat: latitude, lng: longitude };

    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById("geocode").value;

    geocoder.geocode({'address':address}, function getCoordinates( results, status ) {
        if( status == google.maps.GeocoderStatus.OK ) {
            latitude = results[0].geometry.location.lat();
            longitude = results[0].geometry.location.lng();

            map.setCenter( new google.maps.LatLng( latitude,longitude ) ;
        } 
    });

    var map = new google.maps.Map(document.getElementById('map'), {
        center: uluru,
        zoom: 17,
        mapTypeId: 'roadmap'
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Simple example of accessing variables:

<script>
var a = 10;
var c;

function aa() {
  document.write("func aa ");
  var b = 20;
  c = b;
  document.write(a + b);
}

function bb() {
  document.write("func bb ");
  document.write(c);
}

aa();
document.write(" ");
bb();    

Comments

0

From doc of Google Geocoding Service

Accessing the Geocoding service is asynchronous

This means that your function is called after the inizialization of uluru object.

You should inizialize the map and uluru object inside the function getCoordinates

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.