0

I've got a pretty basic function that grabs the users current location. When I alert or log the geoCoder variable I successfully get a value. I'm trying to put that geoCoder value inside of my JSON call.

function userLocation(location, callback) {
    var geoCoder = new google.maps.Geocoder();
        geoCoder.geocode({
            location: location
        }
    console.log(geoCoder);
}

var jsonCall;
jsonCall = '/bin/userlocation/locations.' + geoCoder + '.json'

When I run this I get a geoCoder is not defined js error. How would I go about getting the value of the geoCoder variable? I'm kind of a newb so code examples are appreciated.

3
  • What are you trying to accomplish with this code? Just geocoding an address? Commented Oct 19, 2013 at 23:40
  • The async problem is essentially the same as this one: stackoverflow.com/q/14220321/139010 Commented Oct 19, 2013 at 23:41
  • Correct just geoCode an address when I alert or log the geoCoder variable I successfully get a value i simply need that value outside of the function to put in my json call. Commented Oct 19, 2013 at 23:41

2 Answers 2

1
var jsonCall; //declaring outside and before a function means the var will be accessible inside and outside of the function

function userLocation(location, callback) {
    var geoCoder = new google.maps.Geocoder();
        geoCoder.geocode({
            location: location
        }
    console.log(geoCoder);
    jsonCall = '/bin/userlocation/locations.' + geoCoder + '.json'
}

console.log(jsonCall);
Sign up to request clarification or add additional context in comments.

4 Comments

This is not going to work. The geocode call is Asynchronous.
Switched the vars around so it will work now, thanks!
@BrianOgden Thanks! This worked but out of curiosity why couldn't I put my jsonCall outside the userLocation function and still call the geocoder variable?
so the var geoCoder is a google maps object, the geocode (geoCoder.geocode) method/property/function of the google maps object Geocoder is an asynchronous method and this gives direction to your inquiry but I am not exactly sure myself. Perhaps @AndrewWhitaker could elaborate on why my first answer didn't work but my update to my answer does. Also checkout this question on SO for further research: stackoverflow.com/questions/6129733/…
0

You are creating your variable in function and trying to reach at out of the scope of the function. Try to define it out of the function and assign the value wherever u want.

Define as var geoCoder; out of the function. and fill geoCoder in function.

var geoCoder;
function blabla(){
geocoder = dosomething
}

console.log(geoCoder); // this returns null
blabla();
console.log(geoCoder); // now u can see it is not empty

2 Comments

Okay I'm kind of a newb how would I go about doing that can you please provide a code sample as noted in my question. Thanks!
Note what @Andrew Whitaker said, the geocode call is an Asynchronous function, see my answer, I just declared the jsonCall var before the function call so its in scope inside and outside the function

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.