0

im trying to assign the value of my address to another variable but all i get is undefined or something like my url in the variable

here is my coding. i almost crack my head because of this.

function geoCoding(displayname, trackerModel, setupType, marker, index){


var setupMessageInfoWindow;
var geocoder = new google.maps.Geocoder();

    geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {

            var location = results[1].formatted_address;


        } else {
            alert('No results found at marker ' + marker.position);
        }
    } else {
        alert('Geocoder failed due to: ' + status);
    }
    });



    setupMessageInfoWindow = "<div height=\"300\" width=\"300\"><b>" + displayname + "</b>"
                        + " <br> Location : " + location
                        //+ " <br> Tracker id : " + userid
                        //+ " <br> imei : " + imei
                        + " <br> Tracker Type : " + trackerModel
                        //+ " <br> Mobile Number : " + 
                        //+ " <br> Location : " + location;
                        + " <br> " + setupType
                        + "</div>" ;



    return setupMessageInfoWindow;

}

  • Define `location` as global. Currently it is define as local inside `geocode()` and is not visible outside. – Anto Jurković May 26 '14 at 05:05
  • @AntoJurković - the code does not work. care to give me jsfiddle or something. it state undefined – Keluang Yang Ko Kenal May 26 '14 at 06:37
  • Additional problem is that `geocode()` is asynchronous so `setupMessageInfoWindow` is created before `location` is set. I don't know what is your other code and how/where you will use infowindow message. As solution you can use global variables or callback function. – Anto Jurković May 26 '14 at 07:30
  • What about creating the fiddle by yourself so that people can fork it? – MrUpsidown May 26 '14 at 12:14

1 Answers1

1

geocode is asynchronous so setupMessageInfoWindow variable is created before variable location is properly set. If you want to set some info window you can call a function from geocode() when location is successfully retrieved. For example:

function setContent(marker, content) {
    infoWin.setContent(content);
    google.maps.event.addListener(marker, 'click', function() {
        infoWin.open(map, marker);
    });
}

function geoCoding(displayname, trackerModel, setupType, marker, index){

var setupMessageInfoWindow;
var geocoder = new google.maps.Geocoder();

    geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        console.log('status ok');
        console.log(results);

        if (results[1]) {

            var location = results[1].formatted_address;
            setupMessageInfoWindow = "<div height=\"300\" width=\"300\"><b>" + displayname + "</b>"
                                + " <br> Location : " + location
                                //+ " <br> Tracker id : " + userid
                                //+ " <br> imei : " + imei
                                + " <br> Tracker Type : " + trackerModel
                                //+ " <br> Mobile Number : " + 
                                //+ " <br> Location : " + location;
                                + " <br> " + setupType
                                + "</div>" ;

            setContent(marker, setupMessageInfoWindow);

        } else {
            console.log('No results found at marker ' + marker.position);
        }
    } else {
        console.log('Geocoder failed due to: ' + status);
    }
    });

}

See example at jsbin. There is click event handler set for marker which shows location found.

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42