Jump to content

Passing variables out of a call back function?


sepoto
 Share

Recommended Posts

 <!DOCTYPE html><html><head><title>free.wifi.locations</title><style>#wrapper { margin: 0 auto; background-color: black; }#locationselectblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; }#addressblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; margin-top: 10px; }#locationblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; margin-top: 10px; }#getmapbutton { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: black; width: 90%; margin-top: 10px; }</style><script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=&sensor=false"></script><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script>$(document).ready(function(){ $('#addressblock').hide();$('#locationblock').hide();$('#getmapbutton').hide();$('#map').hide(); $("#radio01").click(function() {if ($(this).is(':checked')) { $('#addressblock').show(); $('#getmapbutton').show(); $('#physichaladdress').focus(); }}); $("#radio02").click(function() {if ($(this).is(':checked')) { $('#locationblock').show(); $('#getmapbutton').show(); }}); $("#theyellow").click(function() { var str = mymapgetter.physichaladdress.value; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();}else{// code for IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200){alert(xmlhttp.responseText);}} if(str != "") { geocoder = new google.maps.Geocoder();var geocoderRequest = { address: str } // //  //  //  //  //geocoder.geocode(geocoderRequest, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { } });// //  //  //  //  // xmlhttp.open("GET","getdata.php?q="+results[0].geometry.location.lat()+results[0].geometry.location.lng(),true);xmlhttp.send();}else { alert("Please enter an address in the text box."); }}); });//end</script> </head><body><div id="wrapper"></br><form name="mymapgetter"><div id="locationselectblock"><h3>How would you like to enter your location data?</h3><input type="radio" name="radio" value="" id="radio01">By physichal address?</input></br><input type="radio" name="radio" value="" id="radio02">By latitude and longitude?</input></div> <div id="addressblock"><h3>Enter your physichal address:</h3></br><input type="text" id="physichaladdress" name="physichaladdress" value="" style="margin-left: 10px; width: 98%;"></input></div> <div id="locationblock"><h3>Enter your latitude and longitude:</h3><input type="text" name="thelat" value=""><- Lat.</input><input type="text" name="thelng" value=""><- Lng.</input></div> <div id="getmapbutton"><input type="button" name="theyellow" id="theyellow" value="getmap" style="width: 100%"></input></form></div></br></div></body></html>

The line in question I will paste below. My console is showing me that there is an error. I am trying to devise a way to get "results[0]" to reside in memory long enough for me to use the array here:

xmlhttp.open("GET","getdata.php?q="+results[0].geometry.location.lat()+results[0].geometry.location.lng(),true);

Unfortunately for me "results[0]" is undefined at that point and I am not really quite sure yet how to get into my parameter 'q'. ?????? Thanks so much!

Link to comment
Share on other sites

I can't see the place where results is being set. You should not access elements like this:

var str = mymapgetter.physichaladdress.value; 

You should access the element by its ID

var str = document.getElementById("physichaladdress").value; 

  • Like 1
Link to comment
Share on other sites

Alright I added that to my code but I'm not seeing how that applies to my question about results[0]. Does anyone have something relevant to how to deal with that situation?

Link to comment
Share on other sites

It becomes set when the function executes. Adding alerts to that function for results[0] and running the code results in two floating point numbers for both and they are in the right range for the U.S.

Link to comment
Share on other sites

That actually is all the code. The code in the .php is a one liner. I am contemplating storing the lat and lng in a cookie although I originally wanted to use a global variable I wasn't able to make it work! The .php is just a one liner: <?phpecho $_GET['q'];?> What happens is when the geocoding function runs it sets results which I verified with alert boxes. Once the scope of the geocoding function is ended then results[0] becomes undefined. I need to keep results[0] around a little bit longer! lol...!

Link to comment
Share on other sites

This is the code I refer to in the above post: // // // // // //geocoder.geocode(geocoderRequest, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { } });// // // // // // It is a callback function and results is set when it runs somehow. It must be some magic of Google.

Edited by sepoto
Link to comment
Share on other sites

This is what I came up with that works:

 <!DOCTYPE html><html><head><title>free.wifi.locations</title><style>#wrapper { margin: 0 auto; background-color: black; }#locationselectblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; }#addressblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; margin-top: 10px; }#locationblock { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: white; width: 90%; margin-top: 10px; }#getmapbutton { margin-left: auto; margin-right: auto; border: 1px solid black; background-color: black; width: 90%; margin-top: 10px; }</style><script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key= &sensor=false"></script><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script>$(document).ready(function(){ $('#addressblock').hide();$('#locationblock').hide();$('#getmapbutton').hide();$('#map').hide(); $("#radio01").click(function() {if ($(this).is(':checked')) { $('#addressblock').show(); $('#getmapbutton').show(); $('#physichaladdress').focus(); }}); $("#radio02").click(function() {if ($(this).is(':checked')) { $('#locationblock').show(); $('#getmapbutton').show(); }}); $("#theyellow").click(function() { var str = document.getElementById("physichaladdress").value; if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();}else{// code for IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function() {if (xmlhttp.readyState==4 && xmlhttp.status==200){alert(xmlhttp.responseText);}} if(str != "") { geocoder = new google.maps.Geocoder();var geocoderRequest = { address: str }var tLat; // //  //  //  //  //geocoder.geocode(geocoderRequest, function(results, status) { if (status == google.maps.GeocoderStatus.OK) {passurl(results[0].geometry.location.lat(),results[0].geometry.location.lng());} return tLat;});// //  //  //  //  //  }else { alert("Please enter an address in the text box."); }}); });//end</script> <script type="text/javascript"> function passurl(tLat, tLng) {xmlhttp.open("GET","getdata.php?q="+tLat+tLng,true);xmlhttp.send();} </script> </head><body><div id="wrapper"></br><form name="mymapgetter"><div id="locationselectblock"><h3>How would you like to enter your location data?</h3><input type="radio" name="radio" value="" id="radio01">By physichal address?</input></br><input type="radio" name="radio" value="" id="radio02">By latitude and longitude?</input></div> <div id="addressblock"><h3>Enter your physichal address:</h3></br><input type="text" id="physichaladdress" name="physichaladdress" value="" style="margin-left: 10px; width: 98%;"></input></div> <div id="locationblock"><h3>Enter your latitude and longitude:</h3><input type="text" name="thelat" value=""><- Lat.</input><input type="text" name="thelng" value=""><- Lng.</input></div> <div id="getmapbutton"><input type="button" name="theyellow" id="theyellow" value="getmap" style="width: 100%"></input></form></div></br></div></body></html>

What I originally had in mind was to do something like: var tLat = new Some.Google.Object(); and then have the object assigned to inside the geocder.geocode callback but it did not work out. I would be most open to any alternate solutions.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...