Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Undefined variable in return. (Rookie Question)
Old 12-10-2008, 08:55 AM Undefined variable in return. (Rookie Question)
Average Talker

Posts: 15
Trades: 0
Hey guys,

This looks like the place to be if you need to find out whats going on when it comes to site coding so I thought I'd drop in this rookie question here. I am playing around with the Google Maps API and the experiment I am working on uses Geocoding to get the co-ords from a place name, and then it will of course place a marker on that spot.

I've taken to it quicker then I thought but now I'm stuck on a seemingly rookie problem. I can't seem to return a value from one function to another. Here is the code snippet with the code concerned. (I have left out the other code as I don't believe its relevant with this issue)

Code:
function geocodeAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              //alert(point) ;
              return point ;
            }
          }
        );
      }
       
    }

function geocodeTest(subject) {
        
        var coords = (geocodeAddress(subject)) ;
        document.getElementById("data").innerHTML = 'Town: ' + subject + '<br />Coords: ' + coords ;
        
    }
With this code it say we run geocodeTest("London") it outputs...

Town: London
Coords: undefined

If I uncomment the alert in the geocodeAddress function it pops up the co-ordinates (52.53523, 0.200241). Thats a close guess for the example btw before anyone points out its wrong!

So its getting the co ordinates but it just won't return them into a variable in geocodeTest().

What am I doing wrong to stop this from returning. Do you need to see the whole code set?

Regards,

Leonard

Last edited by Gentleman_; 12-10-2008 at 08:57 AM..
Gentleman_ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-10-2008, 09:12 AM Re: Undefined variable in return. (Rookie Question)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
variable scoping is the problem

In the code above point is being returned by a method that is local to geocodeAddress() so is NOT available once the local method has closed.

return the value from the geocodeAddress() method to the calling function
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-10-2008, 09:36 AM Re: Undefined variable in return. (Rookie Question)
Average Talker

Posts: 15
Trades: 0
Hey Chris,

Thanks for the quick reply. I've tried a handful of things that I thought you meant but none of them seem to make a difference. I'm kind of following what your on about but its not falling into place yet.

I've tried setting a var coords before the functions and tried to feed point into that to give it to something global before it exits and that makes no difference. I've moved the return code around the function but not found a place where it will output the value of point. Am I returning wrong... the only way I can think of is return point ;. I'm used to PHP and this is melting my brain a bit. Can you offer an example of where I'm going wrong?

Cheers

Leonard

EDIT: I think I have it, I'm not 100% sure what made it start working. I think I put something in while trying to fix it that ultimately broke it, but its returning [Object, object] now which is right I think. Just need remember how to use that now!

If you could fill me in a little more on where I was going wrong it would be much appreciated

EDIT EDIT!:

OK I'm actually stumped on this one. That Object, object seems to have nothing to do with point, if I try and set it as the string "toffee" then is still comes back object Object, in fact if I remove it completely it still does the same. What do I need to be doing?

Last edited by Gentleman_; 12-10-2008 at 11:00 AM.. Reason: Cleaner then posting in response to myself!
Gentleman_ is offline
Reply With Quote
View Public Profile
 
Old 12-10-2008, 03:34 PM Re: Undefined variable in return. (Rookie Question)
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You will need to read some properties from the returned object.

Just about everything in javascript (and all OOP languages) is an object, and those objects have properties (and methods) but it will be most likely the properties that are needed.

You will probably have to look up the documentation for the API, but I would hazard a guess it will be subject.property and coords.property that you need.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Undefined variable in return. (Rookie Question)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53310 seconds with 12 queries