I have declared a local variable inside of getAddress.

When I try to access that variable inside a local function it falls outside its scope.

What am I doing wrong?

function getAddress(latlon, map)
{
    var data = "";
    $("#map-canvas").gmap3({
        marker: {
            latLng: latlon,
            options:{
                draggable:false,
            },
        },
        getaddress:{
            latLng: latlon,
            callback: function(results){
                data = results && results[1] ? results && results[1].formatted_address : "no address";
            },
        },      
    });
    console.log(data); // Outputs empty string
}
Member Avatar for LastMitch

I have declared a local variable inside of getAddress.

Is this related to Google Map?

Member Avatar for LastMitch

How does your div tag looks? Is this for mobile website or regular website?

I am using that example.

However I am wanting to use my local "data" variable past the function.

Member Avatar for LastMitch

However I am wanting to use my local "data" variable past the function.

This line should be content:

content = results && results[1] ? results && results[1].formatted_address : "no address";

not this:

data = results && results[1] ? results && results[1].formatted_address : "no address";

You should read and used this format as your local data:

https://developers.google.com/maps/documentation/javascript/geocoding?hl=fr#GeocodingAddressTypes

I copy and paste the code here for you to see:

 results[]: {
 types[]: string,
 formatted_address: string,
 address_components[]: {
 short_name: string,
 long_name: string,
 types[]: string
 },
 geometry: {
   location: LatLng,
   location_type: GeocoderLocationType
   viewport: LatLngBounds,
   bounds: LatLngBounds
}
}

I've changed my variable back to content; however, that shouldn't matter because it's just a variable name. I understand the data structure of the Google Maps content. I'm trying figure out why my variable looses scope and what I can do to fix it.

Member Avatar for LastMitch

I'm trying figure out why my variable looses scope and what I can do to fix it.

You modify the code from the link. The only thing I notice you took out a few lines.

I don't know what you did. You can't expect someone to fixed (figure out what you did).

The only way to fixed this issue you are having is to write down what changes you made.

I know the code from the link works so you added something to the code or didn't know how add the code or you change the format that it lost the variable.

Yes I did modify the code from the link, I need to complete a different task.
My problem has nothing to do with using that code or my code.
I am wanting to know why I am loosing scope and how to fix it.
I am having a programming languages issue.

Member Avatar for LastMitch

My problem has nothing to do with using that code or my code.

OK

I am wanting to know why I am loosing scope and how to fix it.

Then what is in

var data = " ";

why is it blank?

I also notice your callback function is not connected with the result(not binding).

Explain to me why is not connected(binding)?

if you have something like this:

data.setMarker = function(address) {
    var dataGMap = data;
    data.geocoder.geocode({'address' : address}, function(results, status) {
        dataGMap.map.setCenter(results[0].geometry.location);
        dataGMap.marker.setPosition(results[0].geometry.location);
    });
}

it will make sense.

But you never provide a code from the preivous post and you expect someone to read your mind to see figure out why?

I am intitalizing data to a empty string. That way I don't get a null reference.

I did not provide my code from a previous post.

This is a correct answer.

The ajax method run asynchronously, so the data will still be undefined after the function runs. To fix this I think you should add a "sucess" property to the object and assign it to a callback. Then within that callback you can print out data.

Luckally I posted this question on another site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.