Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    function returns undefined

    hi here is a code i use to calculate distance b//w 2 places using google api...
    it works perfectly and shows the results in the html but when i add a return statement at the end of the function showlocation() it returns undefined.. why it is so.. how to resolve it???

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    	<meta name="robots" content="noindex,follow" />
    	<title>Calculate driving distance with Google Maps API</title>
    	<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAMK3PClIOG6IUkYprx4EfNxSY_HQRLXr6AGORx7Qh39w3-je8JxRROt5eJTcDPJ9nGnVn9xXKTQ2l8Q" type="text/javascript"></script>
    	<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
    	<script type="text/javascript">
    
    	var geocoder, location1,addr1,addr2, location2, result1,gDir;
    function coolAl(add1,add2) {
    addr1=add1;
    addr2=add2;
    var result= return initialize();
    showLocation();
    alert(result);
     }
    	function initialize() {
    		geocoder = new GClientGeocoder();
    		gDir = new GDirections();
    		GEvent.addListener(gDir, "load", function() {
    			var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
    			var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    			result1=location1.address + ' (' + location1.lat + ':' + location1.lon + ')/' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')/' + drivingDistanceKilometers + ' kilometers';
    document.body.innerHTML=result1;
    return drivingDistanceKilometers;
    		});
    	}
    
    	function showLocation() {
    		geocoder.getLocations(addr1, function (response) {
    			if (!response || response.Status.code != 200)
    			{
    				alert("Sorry, we were unable to geocode the first address");
    			}
    			else
    			{
    				location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    				geocoder.getLocations(addr2, function (response) {
    					if (!response || response.Status.code != 200)
    					{
    						alert("Sorry, we were unable to geocode the second address");
    					}
    					else
    					{
    						location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    						gDir.load('from: ' + location1.address + ' to: ' + location2.address);
    					}
    				});
    			}
    		});
    	}
    
    	</script>
    </head>
    
    <body onload="coolAl('pune','mumbai')">
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    This is because the .getLocations() call most probably is asynchronous. Everything you want to do after the .getLocations() finished has to be done (or started from) inside the callback function! A "return" after the .getLocations() will be executed before the method has finished executing!

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    can you help me on how to edit it now...?? it'll be gr8 help..

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Code:
    		geocoder.getLocations(addr1, function (response) {
    			if (!response || response.Status.code != 200)
    			{
    				alert("Sorry, we were unable to geocode the first address");
    			}
    			else
    			{
    				location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    				geocoder.getLocations(addr2, function (response) {
    					if (!response || response.Status.code != 200)
    					{
    						alert("Sorry, we were unable to geocode the second address");
    					}
    					else
    					{
    						location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    						gDir.load('from: ' + location1.address + ' to: ' + location2.address);
    						// Everything you want to do with the request results has to be done here!
    					}
    				});
    			}
    		});

  • Users who have thanked devnull69 for this post:

    arpitbandi (07-27-2011)

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i addedreturn location1.address; to the place you suggested and changed the coolal function to
    Code:
    function coolAl(add1,add2) {
    addr1=add1;
    addr2=add2;
     initialize();
    var result=showLocation();
    alert(result);
     }
    but still it says undefined...
    sorry for being dumb but im a newbie

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    no no no you can not return anything from there. You have to process the result on that exact place OR call a function/method where you give the request result as a parameter. A return will just do nothing here (there is no longer a function to return from actually).

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    k.. then can you please tell the code to take these values to the coolal function?? or may be any tutorial on how to do it.. i read a little but wasnt able to get at all..

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You started the requests from the coolAl() function already so there's no need to go back to that function. Instead you should write another function that takes the result and does whatever you want with this result. Then you call the new function from the place I suggested earlier.

  • #9
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by arpitbandi View Post
    can you help me on how to edit it now...?? it'll be gr8 help..
    This might be a solution ?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    	<meta name="robots" content="noindex,follow" />
    	<title>Calculate driving distance with Google Maps API</title>
    	<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAMK3PClIOG6IUkYprx4EfNxSY_HQRLXr6AGORx7Qh39w3-je8JxRROt5eJTcDPJ9nGnVn9xXKTQ2l8Q" type="text/javascript"></script>
    	<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
    	<script type="text/javascript">
    
    	var geocoder, location1,addr1,addr2, location2, result1,gDir;
    function coolAl(arg) {
    
    alert(arg);
     }
    	function initialize() {
    		geocoder = new GClientGeocoder();
    		gDir = new GDirections();
    		GEvent.addListener(gDir, "load", function() {
    			var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
    			var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
    			result1=location1.address + ' (' + location1.lat + ':' + location1.lon + ')/' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')/' + drivingDistanceKilometers + ' kilometers';
    document.body.innerHTML=result1;
    coolAl(result1)
    return drivingDistanceKilometers;
    		});
    	}
    
    	function showLocation(addr1,addr2) {
    		geocoder.getLocations(addr1, function (response) {
    			if (!response || response.Status.code != 200)
    			{
    				alert("Sorry, we were unable to geocode the first address");
    			}
    			else
    			{
    				location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    				geocoder.getLocations(addr2, function (response) {
    					if (!response || response.Status.code != 200)
    					{
    						alert("Sorry, we were unable to geocode the second address");
    					}
    					else
    					{
    						location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
    						gDir.load('from: ' + location1.address + ' to: ' + location2.address);
    					}
    				});
    			}
    		});
    	}
    
    	</script>
    </head>
    
    <body onload="initialize();showLocation('pune','mumbai')">
    </html>

  • #10
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    try to use the finite variables and then define the default value of the variables as zero so that they wont return infinite value.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •